VR.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. local ReceiverBase = require("core:erm.ReceiverBase")
  2. local VR = ReceiverBase:new()
  3. function VR:new(ERM, v)
  4. assert(v ~= nil, "!!VR requires variable identifier")
  5. return ReceiverBase.new(self, {v=v, ERM=ERM})
  6. end
  7. local match = string.match
  8. local function trim(s)
  9. return match(s,'^()%s*$') and '' or match(s,'^%s*(.*%S)')
  10. end
  11. function VR:H(flagIndex)
  12. local v = trim(self.v)
  13. self.ERM.F[flagIndex] = v ~= ''
  14. end
  15. function VR:U(subString)
  16. self.ERM.F['1'] = string.find(self.v, subString) > 0
  17. end
  18. function VR:M1(startIndex, length)
  19. return string.sub(self.v, startIndex - 1, startIndex - 1 + length)
  20. end
  21. function VR:M2(wordIndex)
  22. local words = string.gmatch(self.v, "[^%s]+")
  23. local i = 0
  24. for w in words do
  25. if i == wordIndex then
  26. return w
  27. end
  28. i = i + 1
  29. end
  30. end
  31. function VR:M3(val, radix)
  32. radix = radix or 10
  33. if(type(val) ~= "number") then
  34. error("The first parameter should be of numeric type")
  35. end
  36. if(type(radix) ~= "number") then
  37. error("The second parameter should be of numeric type. Default value is 10.")
  38. end
  39. if radix == 10 then
  40. return tostring(val)
  41. elseif radix == 16 then
  42. return string.format("%x", val)
  43. else
  44. error("The second parameter value is invalid. Only 10 and 16 radix are supported for now.")
  45. end
  46. end
  47. function VR:M4()
  48. return string.len(self.v)
  49. end
  50. function VR:M5()
  51. local firstPos = string.find(str, "[^%s]+")
  52. return firstPos
  53. end
  54. function VR:M6()
  55. local lastPos = 1 + string.len(self.v) - string.find(string.reverse(self.v), "[^%s]+")
  56. return lastPos
  57. end
  58. return VR