1 -- --- T2-COPYRIGHT-NOTE-BEGIN ---
2 -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 -- T2 SDE: package/.../lua-pcre/string.lua
5 -- Copyright (C) 2006 Juergen "George" Sawinski
7 -- More information can be found in the files COPYING and README.
9 -- This program is free software; you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as published by
11 -- the Free Software Foundation; version 2 of the License. A copy of the
12 -- GNU General Public License can be found in the file COPYING.
13 -- --- T2-COPYRIGHT-NOTE-END ---
17 --[[ TODO -------------------------------------------------------------------
18 - describe string.{cflags,eflags}
19 - gsub does not handle eflags
20 --]] ------------------------------------------------------------------------
22 --[[ DESCRIPTION ------------------------------------------------------------
24 This file replaces the default string library routines "find", "gsub",
25 "gmatch" with mostly similiarly behaving equivalents with regular
26 expression syntax patterns.
28 The original functions are stored in the "string.orig" table.
32 string.study([<boolean>])
34 This function causes the string replacement functions to store the
35 regular expression if the argument is empty or "true". The default
36 compile and execution flags cannot be changed then, however.
38 Supplying "false" as argument clears the regular expression store.
44 --]] ------------------------------------------------------------------------
46 -- original string library functions
50 gmatch
= string.gmatch
,
53 -- default compile and excuation flags
54 local __default_flags__
= {
59 -- string.study -------------------------------------------------------------
62 local function study_regex_new(pattern
)
63 assert(__study__
, "study_regex_new called outside study mode.")
65 if not __study__
[pattern
] then
66 __study__
[pattern
] = @RX@
.new(pattern
, __default_flags__
.compile
)
69 return __study__
[pattern
]
72 local function regex_new(pattern
)
73 return @RX@
.new(pattern
, __default_flags__
.compile
)
78 function string.study(what
)
79 if what
== nil then what
= true end
82 if not __study__
then __study__
= {} end
86 for k
,v
in pairs(__study__
) do
96 -- string.cflags ------------------------------------------------------------
97 function string.cflags(...)
98 if __study__
then error("unable to set compile flags in study mode") end
100 __default_flags__
.compile
= @RX@
.flags(arg
)
103 -- string.cflags ------------------------------------------------------------
104 function string.eflags(...)
105 if __study__
then error("unable to set execution flags in study mode") end
107 __default_flags__
.execute = @RX@
.flags(arg
)
110 -- string.find regex replacement --------------------------------------------
111 function string.find(str
, pattern
, start
, plain
)
112 local idx_start
, idx_end
, capture
115 return string.orig
.find(s
, pat
, init
, plain
)
118 idx_start
, idx_end
, capture
=
119 RX(pattern
):match(str
, start
, __default_flags__
.execute)
121 return idx_start
,idx_end
,unpack(capture
)
124 -- string.gmatch regex replacement ------------------------------------------
126 -- simple result table iterator
127 local function @RX@
_next(t
)
132 if row
then return unpack(row
) end
136 function string.gmatch(str
, pattern
)
137 local t
= {} -- table of result tables
139 local function capture(str
, sub
) -- record results in t
144 RX(pattern
):gmatch(str
, capture
, __default_flags__
.execute)
146 -- return our own simple iterator
150 -- string.gsub regex replacement --------------------------------------------
151 function string.gsub(str
, pat
, repl
, n
)
152 -- FIXME execution flags
153 return generic_gsub(RX
, str
, pat
, repl
, n
)
156 -- original lrexlib/gsub.lua: