Dash:
[t2.git] / package / lua / lua-pcre / string.lua
blobb4bff47d3ac44dbef2fc1ea73d01852fe41aac63
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 Juergen "George" Sawinski
6 --
7 -- More information can be found in the files COPYING and README.
8 --
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 ---
15 require "@RX@"
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.
30 Additional methods:
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.
40 string.cflags(...)
42 string.eflags(...)
44 --]] ------------------------------------------------------------------------
46 -- original string library functions
47 string.orig = {
48 find = string.find,
49 gsub = string.gsub,
50 gmatch = string.gmatch,
53 -- default compile and excuation flags
54 local __default_flags__ = {
55 compile = nil,
56 execute = nil,
59 -- string.study -------------------------------------------------------------
60 local __study__ = nil
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)
67 end
69 return __study__[pattern]
70 end
72 local function regex_new(pattern)
73 return @RX@.new(pattern, __default_flags__.compile)
74 end
76 local RX = regex_new
78 function string.study(what)
79 if what == nil then what = true end
81 if what then
82 if not __study__ then __study__ = {} end
83 RX = study_regex_new
84 else
85 if __study__ then
86 for k,v in pairs(__study__) do
87 v:__gc()
88 end
89 end
91 __study__ = nil
92 RX = regex_new
93 end
94 end
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
114 if plain then
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)
128 if t then
129 local row = t[1]
130 table.remove(t, 1)
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
140 table.insert(t, sub)
143 -- execute regex
144 RX(pattern):gmatch(str, capture, __default_flags__.execute)
146 -- return our own simple iterator
147 return @RX@_next, t
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: