Author: bklaas
Date: Wed Jan 28 13:46:53 2009
New Revision: 3974

URL: http://svn.slimdevices.com?rev=3974&root=Jive&view=rev
Log:
Bug: n/a
Description: make class an extention of Lua's builtin string.* methods
add matchLiteral() method, which is identical to string.match but does not 
interpret magic characters

don't pull in self in split() method

svk requires checkin of changes before svk move, so next checkin will move 
strings.lua to string.lua

Modified:
    7.4/trunk/squeezeplay/src/squeezeplay/share/jive/utils/strings.lua

Modified: 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/utils/strings.lua
URL: 
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/share/jive/utils/strings.lua?rev=3974&root=Jive&r1=3973&r2=3974&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/utils/strings.lua 
(original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/share/jive/utils/strings.lua Wed Jan 
28 13:46:53 2009
@@ -5,11 +5,13 @@
 --[[
 =head1 NAME
 
-jive.util.strings - string utilities
+jive.util.string - string utilities
 
 =head1 DESCRIPTION
 
 Assorted utility functions for strings
+
+Builds on Lua's built-in string.* class 
 
 =head1 SYNOPSIS
 
@@ -21,8 +23,16 @@
 =cut
 --]]
 
-module(..., package.seeall)
 
+local setmetatable = setmetatable
+local table  = require('jive.utils.table')
+local log    = require("jive.utils.log").logger("applets.setup")
+local ltable = require("string")
+
+module(...)
+
+-- this is the bit that does the extension.
+setmetatable(_M, { __index = ltable })
 
 --[[
 
@@ -34,11 +44,11 @@
 --]]
 function str2hex(s)
        local s_hex = ""
-       string.gsub(
+       ltable.gsub(
                        s,
                        "(.)",
                        function (c)
-                               s_hex = s_hex .. string.format("%02X 
",string.byte(c)) 
+                               s_hex = s_hex .. ltable.format("%02X 
",ltable.byte(c)) 
                        end)
        return s_hex
 end
@@ -53,7 +63,7 @@
 =cut
 --]]
 function trim(s)
-       local b, e = string.find(s, "%z")
+       local b, e = ltable.find(s, "%z")
        if b then
                return s:sub(1, b-1)
        else
@@ -63,7 +73,7 @@
 
 --[[
 
-=head2 split(self, inSplitPattern, myString, returnTable)
+=head2 split(inSplitPattern, myString, returnTable)
 
 Takes a string pattern and string as arguments, and an optional third argument 
of a returnTable
 
@@ -72,22 +82,47 @@
 =cut
 --]]
 
-function split(self, inSplitPattern, myString, returnTable)
+function split(inSplitPattern, myString, returnTable)
        if not returnTable then
                returnTable = {}
        end
        local theStart = 1
-       local theSplitStart, theSplitEnd = string.find(myString, 
inSplitPattern, theStart)
+       local theSplitStart, theSplitEnd = ltable.find(myString, 
inSplitPattern, theStart)
        while theSplitStart do
-               table.insert(returnTable, string.sub(myString, theStart, 
theSplitStart-1))
+               table.insert(returnTable, ltable.sub(myString, theStart, 
theSplitStart-1))
                theStart = theSplitEnd + 1
-               theSplitStart, theSplitEnd = string.find(myString, 
inSplitPattern, theStart)
+               theSplitStart, theSplitEnd = ltable.find(myString, 
inSplitPattern, theStart)
        end
-       table.insert(returnTable, string.sub(myString, theStart))
+       table.insert(returnTable, ltable.sub(myString, theStart))
        return returnTable
 end
 
 --[[
+
+=head2 matchLiteral(s, pattern [, init])
+
+Identical to Lua's string.match() method, but escapes all special characters 
in the substring first
+
+Looks for the first match of pattern in the string s. 
+
+If it finds one, then matchLiteral returns the captures from the pattern; 
otherwise it returns nil. 
+
+If pattern specifies no captures, then the whole match is returned. 
+
+A third, optional numerical argument init specifies where to start the search; 
its default value is 1 and can be negative. 
+
+=cut
+--]]
+
+function matchLiteral(s, pattern, init)
+       -- first escape all special characters in pattern
+       local escapedPattern = ltable.gsub(pattern, 
"[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1")
+       return ltable.match(s, escapedPattern, init)
+ 
+end
+
+--[[
+
 
 =head1 LICENSE
 

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to