diff options
author | Marius <mariausol@gmail.com> | 2010-12-04 11:00:16 +0200 |
---|---|---|
committer | Marius <mariausol@gmail.com> | 2010-12-04 11:00:16 +0200 |
commit | 5e7d254e26fb7683947962997410d89b502645b2 (patch) | |
tree | 776e7ae97df073169aef37f3abd27cb9de845ed3 /tex/context/base/util-str.lua | |
parent | a7f5f74dddd5ac70198b0504c82ed105fff71ef3 (diff) | |
download | context-5e7d254e26fb7683947962997410d89b502645b2.tar.gz |
beta 2010.12.04 09:44
Diffstat (limited to 'tex/context/base/util-str.lua')
-rw-r--r-- | tex/context/base/util-str.lua | 88 |
1 files changed, 59 insertions, 29 deletions
diff --git a/tex/context/base/util-str.lua b/tex/context/base/util-str.lua index 18caa27bd..adcbf1654 100644 --- a/tex/context/base/util-str.lua +++ b/tex/context/base/util-str.lua @@ -10,8 +10,9 @@ utilities = utilities or {} utilities.strings = utilities.strings or { } local strings = utilities.strings -local find, gsub = string.find, string.gsub -local patterns, Cs, lpegmatch = lpeg.patterns, lpeg.Cs, lpeg.match +local find, gsub, rep = string.find, string.gsub, string.rep +local Cs, C, Cp, P, Carg = lpeg.Cs, lpeg.C, lpeg.Cp, lpeg.P, lpeg.Carg +local patterns, lpegmatch = lpeg.patterns, lpeg.match -- str = " \n \ntest \n test\ntest " -- print("["..string.gsub(string.collapsecrlf(str),"\n","+").."]") @@ -30,37 +31,72 @@ function strings.collapsecrlf(str) return lpegmatch(pattern,str) end +-- The following functions might end up in another namespace. + +--~ function strings.tabtospace(str,tab) +--~ -- we don't handle embedded newlines +--~ while true do +--~ local s = find(str,"\t") +--~ if s then +--~ if not tab then tab = 7 end -- only when found +--~ local d = tab-(s-1) % tab +--~ if d > 0 then +--~ str = gsub(str,"\t",rep(" ",d),1) +--~ else +--~ str = gsub(str,"\t","",1) +--~ end +--~ else +--~ break +--~ end +--~ end +--~ return str +--~ end + +local extra, tab, start = 0, 0, 4, 0 + +local pattern = + Carg(1) / function(t) + extra, tab, start = 0, t or 7, 1 + end + * Cs(( + Cp() * patterns.tab / function(position) + local current = (position - start + 1) + extra + local spaces = tab-(current-1) % tab + if spaces > 0 then + extra = extra + spaces - 1 + return rep(" ",spaces) + else + return "" + end + end + + patterns.newline * Cp() / function(position) + extra, start = 0, position + end + + patterns.anything + )^1) + +function strings.tabtospace(str,tab) + return lpegmatch(pattern,str,1,tab or 7) +end + --~ local t = { --~ "1234567123456712345671234567", +--~ "\tb\tc", --~ "a\tb\tc", --~ "aa\tbb\tcc", --~ "aaa\tbbb\tccc", --~ "aaaa\tbbbb\tcccc", --~ "aaaaa\tbbbbb\tccccc", ---~ "aaaaaa\tbbbbbb\tcccccc", +--~ "aaaaaa\tbbbbbb\tcccccc\n aaaaaa\tbbbbbb\tcccccc", +--~ "one\n two\nxxx three\nxx four\nx five\nsix", --~ } ---~ for k,v do ---~ print(string.tabtospace(t[k])) +--~ for k=1,#t do +--~ print(strings.tabtospace(t[k])) --~ end --- The following functions might end up in another namespace. - -function strings.tabtospace(str,tab) - -- we don't handle embedded newlines - while true do - local s = find(str,"\t") - if s then - if not tab then tab = 7 end -- only when found - local d = tab-(s-1) % tab - if d > 0 then - str = gsub(str,"\t",rep(" ",d),1) - else - str = gsub(str,"\t","",1) - end - else - break - end - end +function strings.striplong(str) -- strips all leading spaces + str = gsub(str,"^%s*","") + str = gsub(str,"[\n\r]+ *","\n") return str end @@ -69,9 +105,3 @@ end --~ bb --~ cccccc --~ ]]) - -function strings.striplong(str) -- strips all leading spaces - str = gsub(str,"^%s*","") - str = gsub(str,"[\n\r]+ *","\n") - return str -end |