summaryrefslogtreecommitdiff
path: root/tex/context/base/util-str.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2010-11-20 00:20:11 +0200
committerMarius <mariausol@gmail.com>2010-11-20 00:20:11 +0200
commitb4b58bbfe882406b09b2548e7aa55d238987f894 (patch)
tree346fdd16f41f0d24406a64e7167d4dade8e1b325 /tex/context/base/util-str.lua
parentc59707ed27b62aa6d05dccba192d134de4ec24f6 (diff)
downloadcontext-b4b58bbfe882406b09b2548e7aa55d238987f894.tar.gz
beta 2010.11.19 22:50
Diffstat (limited to 'tex/context/base/util-str.lua')
-rw-r--r--tex/context/base/util-str.lua77
1 files changed, 77 insertions, 0 deletions
diff --git a/tex/context/base/util-str.lua b/tex/context/base/util-str.lua
new file mode 100644
index 000000000..18caa27bd
--- /dev/null
+++ b/tex/context/base/util-str.lua
@@ -0,0 +1,77 @@
+if not modules then modules = { } end modules ['util-str'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+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
+
+-- str = " \n \ntest \n test\ntest "
+-- print("["..string.gsub(string.collapsecrlf(str),"\n","+").."]")
+
+local rubish = patterns.spaceortab^0 * patterns.newline
+local anyrubish = patterns.spaceortab + patterns.newline
+local anything = patterns.anything
+local stripped = (patterns.spaceortab^1 / "") * patterns.newline
+local leading = rubish^0 / ""
+local trailing = (anyrubish^1 * patterns.endofstring) / ""
+local redundant = rubish^3 / "\n"
+
+local pattern = Cs(leading * (trailing + redundant + stripped + anything)^0)
+
+function strings.collapsecrlf(str)
+ return lpegmatch(pattern,str)
+end
+
+--~ local t = {
+--~ "1234567123456712345671234567",
+--~ "a\tb\tc",
+--~ "aa\tbb\tcc",
+--~ "aaa\tbbb\tccc",
+--~ "aaaa\tbbbb\tcccc",
+--~ "aaaaa\tbbbbb\tccccc",
+--~ "aaaaaa\tbbbbbb\tcccccc",
+--~ }
+--~ for k,v do
+--~ print(string.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
+ return str
+end
+
+--~ local template = string.striplong([[
+--~ aaaa
+--~ bb
+--~ cccccc
+--~ ]])
+
+function strings.striplong(str) -- strips all leading spaces
+ str = gsub(str,"^%s*","")
+ str = gsub(str,"[\n\r]+ *","\n")
+ return str
+end