OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.2 KiB

  1. -- --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. --
  4. -- T2 SDE: package/.../lua-pcre/string.lua
  5. -- Copyright (C) 2006 The T2 SDE Project
  6. -- Copyright (C) 2006 Juergen "George" Sawinski
  7. --
  8. -- More information can be found in the files COPYING and README.
  9. --
  10. -- This program is free software; you can redistribute it and/or modify
  11. -- it under the terms of the GNU General Public License as published by
  12. -- the Free Software Foundation; version 2 of the License. A copy of the
  13. -- GNU General Public License can be found in the file COPYING.
  14. -- --- T2-COPYRIGHT-NOTE-END ---
  15. require "@RX@"
  16. --[[ TODO -------------------------------------------------------------------
  17. - describe string.{cflags,eflags}
  18. - gsub does not handle eflags
  19. --]] ------------------------------------------------------------------------
  20. --[[ DESCRIPTION ------------------------------------------------------------
  21. This file replaces the default string library routines "find", "gsub",
  22. "gmatch" with mostly similiarly behaving equivalents with regular
  23. expression syntax patterns.
  24. The original functions are stored in the "string.orig" table.
  25. Additional methods:
  26. string.study([<boolean>])
  27. This function causes the string replacement functions to store the
  28. regular expression if the argument is empty or "true". The default
  29. compile and execution flags cannot be changed then, however.
  30. Supplying "false" as argument clears the regular expression store.
  31. string.cflags(...)
  32. string.eflags(...)
  33. --]] ------------------------------------------------------------------------
  34. -- original string library functions
  35. string.orig = {
  36. find = string.find,
  37. gsub = string.gsub,
  38. gmatch = string.gmatch,
  39. }
  40. -- default compile and excuation flags
  41. local __default_flags__ = {
  42. compile = nil,
  43. execute = nil,
  44. }
  45. -- string.study -------------------------------------------------------------
  46. local __study__ = nil
  47. local function study_regex_new(pattern)
  48. assert(__study__, "study_regex_new called outside study mode.")
  49. if not __study__[pattern] then
  50. __study__[pattern] = @RX@.new(pattern, __default_flags__.compile)
  51. end
  52. return __study__[pattern]
  53. end
  54. local function regex_new(pattern)
  55. return @RX@.new(pattern, __default_flags__.compile)
  56. end
  57. local RX = regex_new
  58. function string.study(what)
  59. if what == nil then what = true end
  60. if what then
  61. if not __study__ then __study__ = {} end
  62. RX = study_regex_new
  63. else
  64. if __study__ then
  65. for k,v in pairs(__study__) do
  66. v:__gc()
  67. end
  68. end
  69. __study__ = nil
  70. RX = regex_new
  71. end
  72. end
  73. -- string.cflags ------------------------------------------------------------
  74. function string.cflags(...)
  75. if __study__ then error("unable to set compile flags in study mode") end
  76. __default_flags__.compile = @RX@.flags(arg)
  77. end
  78. -- string.cflags ------------------------------------------------------------
  79. function string.eflags(...)
  80. if __study__ then error("unable to set execution flags in study mode") end
  81. __default_flags__.execute = @RX@.flags(arg)
  82. end
  83. -- string.find regex replacement --------------------------------------------
  84. function string.find(str, pattern, start, plain)
  85. local idx_start, idx_end, capture
  86. if plain then
  87. return string.orig.find(s, pat, init, plain)
  88. end
  89. idx_start, idx_end, capture =
  90. RX(pattern):match(str, start, __default_flags__.execute)
  91. return idx_start,idx_end,unpack(capture)
  92. end
  93. -- string.gmatch regex replacement ------------------------------------------
  94. -- simple result table iterator
  95. local function @RX@_next(t)
  96. if t then
  97. local row = t[1]
  98. table.remove(t, 1)
  99. if row then return unpack(row) end
  100. end
  101. end
  102. function string.gmatch(str, pattern)
  103. local t = {} -- table of result tables
  104. local function capture(str, sub) -- record results in t
  105. table.insert(t, sub)
  106. end
  107. -- execute regex
  108. RX(pattern):gmatch(str, capture, __default_flags__.execute)
  109. -- return our own simple iterator
  110. return @RX@_next, t
  111. end
  112. -- string.gsub regex replacement --------------------------------------------
  113. function string.gsub(str, pat, repl, n)
  114. -- FIXME execution flags
  115. return generic_gsub(RX, str, pat, repl, n)
  116. end
  117. -- original lrexlib/gsub.lua: