Advertisement
theoriginalbit

StringX

Jan 24th, 2013
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.36 KB | None | 0 0
  1. --[[
  2.   StringX v2.3.1 [Updated: 8 Feb 2013]
  3.  
  4.   Copyright © 2013-2014 Joshua Asbury a.k.a. theoriginalbit [theoriginalbit@gmail.com]
  5.  
  6.   Permission is hereby granted, free of charge, to any person obtaining a copy
  7.   of this software and associated documentation files (the "Software"), to deal
  8.   in the Software without restriction, including without limitation the rights
  9.   to use, copy, modify, merge, publish, distribute, and/or sublicense copies
  10.   of the Software, and to permit persons to whom the Software is furnished to
  11.   do so, subject to the following conditions:
  12.  
  13.   - The above copyright notice and this permission notice shall be included in
  14.     all copies or substantial portions of the Software;
  15.   - Visible credit is given to the original author;
  16.   - The software is distributed in a non-profit way;
  17.  
  18.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24.   THE SOFTWARE.
  25. --]]
  26.  
  27. --[[
  28. Java methods from http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
  29. ]]--
  30.  
  31. function charAt( str, index )
  32.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  33.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  34.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  35.     return str:sub( index, index )
  36. end
  37.  
  38. function compareTo( oneString, anotherString )
  39.     if type( oneString ) ~= "string" then error( "Bad argument: String expected, got "..type( oneString ), 2 ) end
  40.     if type( anotherString ) ~= "string" then error( "Bad argument: String expected, got "..type( anotherString ), 2 ) end
  41.     return oneString == anotherString and 0 or oneString:len() - anotherString:len()
  42. end
  43.  
  44. function compareToIgnoreCase( oneString, anotherString )
  45.     if type( oneString ) ~= "string" then error( "Bad argument: String expected, got "..type( oneString ), 2 ) end
  46.     if type( anotherString ) ~= "string" then error( "Bad argument: String expected, got "..type( anotherString ), 2 ) end
  47.     return oneString:lower() == anotherString:lower() and 0 or oneString:len() - anotherString:len()
  48. end
  49.  
  50. function concat( oneString, anotherString )
  51.     if type( oneString ) ~= "string" then error( "Bad argument: String expected, got "..type( oneString ), 2 ) end
  52.     if type( anotherString ) ~= "string" then error( "Bad argument: String expected, got "..type( anotherString ), 2 ) end
  53.     return oneString..anotherString
  54. end
  55.  
  56. function contains( str, seq )
  57.     if type( str ) == nil then error( "Bad argument: Value expected, got nil", 2 ) end
  58.     str = tostring(str)
  59.     local sStart, sEnd, sStr = str:find( seq, 1 )
  60.     return sStart ~= nil
  61. end
  62.  
  63. function contentEquals( str, tData )
  64.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  65.     if type( tData ) ~= "table" then error( "Bad argument: Table expected, got "..type( tData ), 2 ) end
  66.     return str == table.concat( tData )
  67. end
  68.  
  69. function copyValueOf( tData )
  70.     if type( tData ) ~= "table" then error( "Bad argument: Table expected, got "..type( tData ), 2 ) end
  71.     return table.concat( tData )
  72. end
  73.  
  74.  
  75. function copySubsetOf( tData, offset, count )
  76.     if type( tData ) ~= "table" then error( "Bad argument: Table expected, got "..type( tData ), 2 ) end
  77.     if type( offset ) ~= "number" then error( "Bad argument: Number expected, got "..type( offset ), 2 ) end
  78.     if type( count ) ~= "number" then error( "Bad argument: Number expected, got "..type( count ), 2 ) end
  79.     if offset + count - 1 > #tData then error( "Bad argument: Index out of bounds: "..( offset + count - 1), 2 ) end
  80.     return table.concat( tData, "", offset, offset + count - 1 )
  81. end
  82.  
  83. function endsWith( str, suffix )
  84.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  85.     return str:sub( str:len() - suffix:len() + 1) == tostring( suffix )
  86. end
  87.  
  88. function equals( str, value )
  89.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  90.     return str == tostring( value )
  91. end
  92.  
  93. function equalsIgnoreCase( oneStr, anotherStr )
  94.     if type( oneStr ) ~= "string" then error( "Bad argument: String expected, got "..type( oneStr ), 2 ) end
  95.     if type( anotherStr ) ~= "string" then error( "Bad argument: String expected, got "..type( anotherStr ), 2 ) end
  96.    
  97.     return oneStr:lower() == anotherStr:lower()
  98. end
  99.  
  100. function getBytes( str )
  101.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  102.     return { string.byte(str, 1, #str) }
  103. end
  104.  
  105. function getChars( str )
  106.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  107.     local chars = {}
  108.     for i = 1, str:len() do
  109.         chars[i] = str:sub( i, i )
  110.     end
  111.     return chars
  112. end
  113.  
  114. --[[
  115. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#hashCode()
  116.  
  117. Returns a hash code for this string. The hash code for a String object is computed as
  118.  
  119.     s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
  120.  
  121. using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
  122. ]]--
  123.  
  124. function hashCode( str )
  125.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  126.     local int = 0
  127.     for i = 1, #str do
  128.         int = int + str:byte( i ) * ( 31 ^ ( #str - i ) )
  129.     end
  130.     return int
  131. end
  132.  
  133. function indexOf( str, ch )
  134.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  135.     ch = tostring( ch )
  136.     if #ch > 1 then error( "Bad argument: Char expected, got string, consider using indexOfSubstring if intentional", 2 ) end
  137.     for i = 1, #str do
  138.         if str:sub( i, i ) == ch then
  139.             return i
  140.         end
  141.     end
  142.     return nil
  143. end
  144.  
  145. function indexOfAfter( str, ch, index )
  146.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  147.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  148.     ch = tostring( ch )
  149.     if #ch > 1 then error( "Bad argument: Char expected, got string, consider using indexOfSubstring if intentional", 2 ) end
  150.     for i = index + 1, #str do
  151.         if str:sub( i, i ) == ch then
  152.             return i
  153.         end
  154.     end
  155.     return nil
  156. end
  157.  
  158. function indexOfSubstring( str, sub )
  159.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  160.     sub = tostring( sub )
  161.     local sStart, sEnd, sStr = str:find( sub, 1 )
  162.     return sStart
  163. end
  164.  
  165. function indexOfSubstringFrom( str, sub, index )
  166.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  167.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  168.     sub = tostring( sub )
  169.     local sStart, sEnd, sStr = str:find( sub, index + 1 )
  170.     return sStart
  171. end
  172.  
  173. function isEmpty( str )
  174.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  175.     return #str == 0
  176. end
  177.  
  178. function lastIndexOf( str, ch )
  179.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  180.     ch = tostring( ch )
  181.     if #ch > 1 then error( "Bad argument: Char expected, got string", 2 ) end
  182.     for i = #str, 1, -1 do
  183.         if str:sub( i, i) == ch then
  184.             return i
  185.         end
  186.     end
  187.     return nil
  188. end
  189.  
  190. function lastIndexOfFrom( str, ch, index )
  191.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  192.     ch = tostring( ch )
  193.     if #ch > 1 then error( "Bad argument: Char expected, got string", 2 ) end
  194.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  195.     for i = index - 1, 1, -1 do
  196.         if str:sub( i, i) == ch then
  197.             return i
  198.         end
  199.     end
  200.     return nil
  201. end
  202.  
  203. function lastIndexOfSubstring( str, sub )
  204.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  205.     sub = tostring( sub )
  206.     for i = #str - #sub, 1, -1 do
  207.         local s = str:sub( i, i + #sub - 1 )
  208.         if s == sub then
  209.             return i
  210.         end
  211.     end
  212.     return nil
  213. end
  214.  
  215. function lastIndexOfSubstringFrom( str, sub, index )
  216.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  217.     sub = tostring( sub )
  218.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  219.     for i = #str - (#str - index) - #sub - 1, 1, -1 do
  220.         local s = str:sub( i, i + #sub - 1 )
  221.         if s == sub then
  222.             return i
  223.         end
  224.     end
  225.     return nil
  226. end
  227.  
  228. function regionMatches( str, sOffset, other, oOffset, len)
  229.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  230.     if type( other ) ~= "string" then error( "Bad argument: String expected, got "..type( other ), 2 ) end
  231.     if type( sOffset ) ~= "number" then error( "Bad argument: Number expected, got "..type( sOffset ), 2 ) end
  232.     if type( oOffset ) ~= "number" then error( "Bad argument: Number expected, got "..type( oOffset ), 2 ) end
  233.     if type( len ) ~= "number" then error( "Bad argument: Number expected, got "..type( len ), 2 ) end
  234.     if sOffset > #str then error( "Bad argument: Offset cannot be larger than string length", 2 ) end
  235.     if oOffset > #other then error( "Bad argument: Offset cannot be larger than string length", 2 ) end
  236.     if len > #str or len > #other then error( "Bad argument: Length cannot be longer than the shortest string", 2 ) end
  237.     return ( str:sub( sOffset, sOffset ) == other:sub( oOffset, oOffset ) )
  238. end
  239.    
  240. function replace( str, old, new )
  241.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  242.     old = tostring( old )
  243.     new = tostring( new )
  244.     local nStr, count = str:gsub( old, new )
  245.     return nStr
  246. end
  247.  
  248. function replaceFirst( str, old, new )
  249.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  250.     old = tostring( old )
  251.     new = tostring( new )
  252.     local nStr, count = str:gsub( old, new, 1 )
  253.     return nStr
  254. end
  255.  
  256. function split( str, regex )
  257.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  258.     if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
  259.     local t = {}
  260.     regex = "[^"..regex.."]+"
  261.     for s in str:gmatch(regex) do
  262.         t[#t+1] = s
  263.     end
  264.     return t
  265. end
  266.  
  267. function splitLimit( str, regex, lim )
  268.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  269.     if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
  270.     if type( lim ) ~= "number" then error( "Bad argument: Number expected, got "..type( lim ), 2 ) end
  271.     local t = { }
  272.     local fpat = "(.-)"..regex
  273.     local last_end = 1
  274.     local s, e, cap = str:find(fpat, 1)
  275.     while s do
  276.         if #t == lim - 1 then
  277.             break
  278.         end
  279.         if s ~= 1 or cap ~= "" then
  280.             table.insert(t,cap)
  281.         end
  282.         last_end = e+1
  283.         s, e, cap = str:find(fpat, last_end)
  284.     end
  285.     if last_end <= #str then
  286.         cap = str:sub(last_end)
  287.         table.insert(t, cap)
  288.     end
  289.     return t
  290. end
  291.  
  292. function startsWith( str, prefix )
  293.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  294.     prefix = tostring( prefix )
  295.     return str:sub( 1, prefix:len()) == prefix
  296. end
  297.  
  298. function subSequence( str, beginIndex, endIndex )
  299.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  300.     if type( beginIndex ) ~= "number" then error( "Bad argument: Number expected, got "..type( beginIndex ), 2 ) end
  301.     if type( endIndex ) ~= "number" then error( "Bad argument: Number expected, got "..type( endIndex ), 2 ) end
  302.     local t = {}
  303.     for i = beginIndex, endIndex do
  304.         table.insert( t, str:sub( i, i ) )
  305.     end
  306.     return t
  307. end
  308.  
  309. function toCharTable( str )
  310.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  311.     return getChars( str )
  312. end
  313.  
  314. function trim( str )
  315.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  316.     return (str:gsub("^%s*(.-)%s*$", "%1"))
  317. end
  318.  
  319. function valueOf( tData )
  320.     if type( tData ) ~= "table" then error( "Bad argument: Table expected, got "..type( tData), 2 ) end
  321.     return table.concat( tData )
  322. end
  323.  
  324. function valueOfWith( tData, offset, count )
  325.     if type( tData ) ~= "table" then error( "Bad argument: Table expected, got "..type( tData), 2 ) end
  326.     if offset == 0 then error( "Bad argument: Table index out of bounds: index "..offset, 2) end
  327.     if offset > #tData then error( "Bad argument: Table index out of bounds: index "..offset.." length "..#tData , 2) end
  328.     if count == nil then count = #tData - offset + 1 end
  329.     if offset + count > #tData + 1 then error( "Bad argument: Offset + Count cannot be larger than table length", 2 ) end
  330.     return table.concat( tData, "", offset, offset + count - 1 )
  331. end
  332.  
  333.  
  334. --[[
  335. C# Methods from http://msdn.microsoft.com/en-us/library/system.string.aspx
  336. ]]--
  337.  
  338. function isWhitespace( str )
  339.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  340.     if str == "" then return false end
  341.     return trim( str ) == ""
  342. end
  343.  
  344. function padLeft( str, count )
  345.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  346.     if type( count ) ~= "number" then error( "Bad argument: Number expected, got "..type( count ), 2 ) end
  347.     count = math.floor( count )
  348.     return string.rep( " ", count )..str
  349. end
  350.  
  351. function padLeftWith( str, count, char )
  352.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  353.     if type( count ) ~= "number" then error( "Bad argument: Number expected, got "..type( count ), 2 ) end
  354.     if type( char ) ~= "string" then error( "Bad argument: String expected, got "..type( char ), 2 ) end
  355.     if #char > 1 then error( "Bad argument: Char expected, got string", 2 ) end
  356.     count = math.floor( count )
  357.     return string.rep( char, count )..str
  358. end
  359.  
  360. function padRight( str, count )
  361.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  362.     if type( count ) ~= "number" then error( "Bad argument: Number expected, got "..type( count ), 2 ) end
  363.     count = math.floor( count )
  364.     return str..string.rep( " ", count )
  365. end
  366.  
  367. function padRightWith( str, count, char )
  368.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  369.     if type( count ) ~= "number" then error( "Bad argument: Number expected, got "..type( count ), 2 ) end
  370.     if type( char ) ~= "string" then error( "Bad argument: String expected, got "..type( char ), 2 ) end
  371.     if #char > 1 then error( "Bad argument: Char expected, got string", 2 ) end
  372.     count = math.floor( count )
  373.     return str..string.rep( char, count )
  374. end
  375.  
  376. function trimEnd( str )
  377.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  378.     return (str:gsub("^(.-)%s*$", "%1"))
  379. end
  380.  
  381. function trimStart( str )
  382.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  383.     return (str:gsub("^%s*(.-)", "%1"))
  384. end
  385.  
  386. --[[
  387. Objective-C Methods from http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/TP40003744
  388. ]]--
  389.  
  390. function stringWithContentsOfFile( path )
  391.     if type( path ) ~= "string" then error( "Bad argument: String expected, got "..type( path ), 2 ) end
  392.     if not fs.exists( path ) then error( "Bad argument: File does not exist at path: "..path, 2 ) end
  393.     if fs.isDir( path ) then error( "Bad argument: Not a file at path: "..path, 2 ) end
  394.    
  395.     local file = io.open( path, "r" )
  396.     local contents = file:read("*a")
  397.     file:close()
  398.    
  399.     return contents
  400. end
  401.  
  402. function stringWithContentsOfURL( url )
  403.     if type( url ) ~= "string" then error( "Bad argument: String expected, got "..type( url ), 2 ) end
  404.     url = textutils.urlEncode( url )
  405.    
  406.     http.request( url )
  407.    
  408.     local event = nil
  409.     while true do
  410.         event = { os.pullEventRaw() }
  411.        
  412.         if (event[1] == "http_success") then
  413.                     break
  414.         elseif (event[1] == "http_failure") then
  415.             return "No response from server."
  416.         end
  417.     end
  418.    
  419.     return event[3].readAll()
  420. end
  421.  
  422. function writeToFile( str, path )
  423.     if type( path ) ~= "string" then error( "Bad argument: String expected, got "..type( path ), 2 ) end
  424.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  425.    
  426.     local file = io.open( path, "w" )
  427.     file:write( str )
  428.     file:close()
  429. end
  430.  
  431. function characterAtIndex( str, index )
  432.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  433.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  434.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  435.     return charAt( index, index )
  436. end
  437.  
  438. function getCharacters( str, range )
  439.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  440.     return getChars( str )
  441. end
  442.  
  443. function componentsSeparatedByString( str, sub )
  444.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  445.     if type( sub ) ~= "string" then error( "Bad argument: String expected, got "..type( sub ), 2 ) end
  446.     return split( str, sub )
  447. end
  448.  
  449. function substringFromIndex( str, index )
  450.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  451.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  452.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  453.     return str:sub( index )
  454. end
  455.  
  456. function substringToIndex( str, index )
  457.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  458.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  459.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  460.     return str:sub( 1, index - 1 )
  461. end
  462.  
  463. function substringWithRange( str, index, len )
  464.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  465.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  466.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  467.     if index + len - 1 > #str then error( "Bad argument: Index + Length out of bounds: "..index, 2 ) end
  468.     return str:sub( index, index + len )
  469. end
  470.  
  471. function rangeOfString( str, sub )
  472.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  473.     sub = tostring( sub )
  474.     local sStart, sEnd, sStr = str:find( sub, 1 )
  475.     return sStart, ( sEnd - sStart )
  476. end
  477.  
  478. function rangeOfStringWithinRange( str, sub, index, len )
  479.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  480.     if type( index ) ~= "number" then error( "Bad argument: Number expected, got "..type( index ), 2 ) end
  481.     if index < 1 or index > #str then error( "Bad argument: Index out of bounds: "..index, 2 ) end
  482.     if index + len - 1 > #str then error( "Bad argument: Index + Length out of bounds: "..index, 2 ) end
  483.     sub = tostring( sub )
  484.     local sStart, sEnd, sStr = str:find( sub, index + 1 )
  485.     return ( sStart > index + len - 1 ) and nil, nil or sStart, ( sEnd - sStart )
  486. end
  487.  
  488. function stringByReplacingOccurrencesOfStringWithString( str, old, new )
  489.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  490.     old = tostring( old )
  491.     new = tostring( new )
  492.     local nStr, count = str:gsub( old, new )
  493.     return nStr
  494. end
  495.  
  496. function capitalizedString( str )
  497.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  498.     local nStr, count = str:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end )
  499.     return nStr
  500. end
  501.  
  502. function uppercaseString( str )
  503.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  504.     return str:upper()
  505. end
  506.  
  507. function lowercaseString( str )
  508.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  509.     return str:lower()
  510. end
  511.  
  512. function intValue( str )
  513.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  514.     local num = tonumber( str )
  515.     return math.floor( num )
  516. end
  517.  
  518. function boolValue( str )
  519.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  520.     if str:lower() == "true" then
  521.         return true
  522.     elseif str:lower() == "false" then
  523.         return false
  524.     else
  525.         return nil
  526.     end
  527. end
  528.  
  529. function pathComponents( str )
  530.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  531.     local c = componentsSeparatedByString( str, "/" )
  532.     local ret = {}
  533.     if startsWith( str, "/" ) then table.insert( ret, "/" ) end
  534.     for i = 1, #c do
  535.         table.insert( ret, c[i] )
  536.     end
  537.     return ret
  538. end
  539.  
  540. function lastPathComponent( str )
  541.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  542.     local components = pathComponents( str )
  543.     return components[ #components ]
  544. end
  545.  
  546. function isAbsolutePath( str )
  547.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  548.     local r = shell.resolve( str )
  549.     return str == ( startsWith( r, "/" ) ) and r or "/"..r
  550. end
  551.  
  552. function pathExtension( str )
  553.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  554.     local file = lastPathComponent( str )
  555.     if not contains( file, "%." ) then return "" end
  556.     local ext = componentsSeparatedByString( file, "%." )
  557.     return ext[ #ext ]
  558. end
  559.  
  560. function stringByAppendingPathComponent( str, comp )
  561.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  562.     if type( comp ) ~= "string" then error( "Bad argument: String expected, got "..type( comp ), 2 ) end
  563.     nStr = str
  564.     if nStr ~= "" then
  565.         if not endsWith( nStr, "/" ) then
  566.             nStr = nStr.."/"
  567.         end
  568.     end
  569.     return nStr..comp
  570. end
  571.  
  572. function stringByAppendingPathExtension( str, ext )
  573.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  574.     if type( ext ) ~= "string" then error( "Bad argument: String expected, got "..type( ext ), 2 ) end
  575.     if endsWith( str, "/" ) then
  576.         str = str:sub( 1, #str - 1 )
  577.     end
  578.     return str..( contains( ext, "%." ) and "" or ".")..ext
  579. end
  580.  
  581. function stringByDeletingLastPathComponent( str )
  582.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  583.     if str == "/" then
  584.         return str
  585.     end
  586.     if contains( str, "/" ) then
  587.         local path = componentsSeparatedByString( str, "/" )
  588.         return "/"..table.concat( path, "/", 1, #path - 1 )
  589.     else
  590.         return ""
  591.     end
  592. end
  593.  
  594. function stringByDeletingPathExtension( str )
  595.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  596.     if str == "/" then
  597.         return str
  598.     end
  599.     if endsWith( str, "/" ) then
  600.         str = str:sub( 1, #str - 1)
  601.     end
  602.     if contains( str, "%." ) then
  603.         local s = replace( str, "."..pathExtension( str ), "" )
  604.         if s == "" then
  605.             return str
  606.         else
  607.             return s
  608.         end
  609.     else
  610.         return str
  611.     end
  612. end
  613.  
  614. function stringByAddingPercentEscapes( str )
  615.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  616.     str = string.gsub (str, "\n", "\r\n")
  617.     str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end )
  618.     str = string.gsub (str, " ", "+")
  619.     return str
  620. end
  621.  
  622. function stringByReplacingPercentEscapes( str )
  623.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  624.     str = string.gsub (str, "+", " ")
  625.     str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
  626.     str = string.gsub (str, "\r\n", "\n")
  627.     return str
  628. end
  629.  
  630. --[[
  631. Following have been added for some ease of use but are not in the Java String class
  632. ]]--
  633.  
  634.  
  635. function containsIgnoreCase( str, seq )
  636.     if type( str ) == nli then error( "Bad argument: Value expected, got nil", 2 ) end
  637.     str = tostring(str):lower()
  638.     return contains( str, seq:lower() )
  639. end
  640.  
  641. function startsWithIgnoreCase( str, prefix )
  642.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  643.     prefix = tostring( prefix )
  644.    
  645.     return startsWith( str:lower(), prefix:lower() )
  646. end
  647.  
  648. function endsWithIgnoreCase( str, suffix )
  649.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  650.     suffix = tostring( suffix )
  651.    
  652.     return endsWith( str:lower(), suffix:lower() )
  653. end
  654.  
  655. function regionMatchesIgnoreCase( str, sOffset, other, oOffset, len)
  656.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  657.     if type( other ) ~= "string" then error( "Bad argument: String expected, got "..type( other ), 2 ) end
  658.     if type( sOffset ) == nil then sOffset = 1 end
  659.     if type( sOffset ) ~= "number" then error( "Bad argument: Number expected, got "..type( sOffset ), 2 ) end
  660.     if type( oOffset ) == nil then oOffset = 1 end
  661.     if type( oOffset ) ~= "number" then error( "Bad argument: Number expected, got "..type( oOffset ), 2 ) end
  662.     if type( len ) ~= "number" then error( "Bad argument: Number expected, got "..type( len ), 2 ) end
  663.    
  664.     return regionMatches( str:lower(), sOffset, other:lower(), oOffset, len )
  665. end
  666.  
  667. function sentenceCase( str )
  668.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  669.     local nStr, count = str:gsub("%a", string.upper, 1)
  670.     return nStr
  671. end
  672.  
  673. function titleCase( str )
  674.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  675.     local nStr, count = str:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end )
  676.     return nStr
  677. end
  678.  
  679. function splitLineToTable( str, width )
  680.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  681.     if type( width ) ~= "number" then error( "Bad argument: Number expected, got "..type( width ), 2 ) end
  682.     if width < 1 then error( "Bad argument: Width must be positive", 2 ) end
  683.     local currLine = ""
  684.     local words = split( str, " " )
  685.     local t = {}
  686.     for i = 1, #words do
  687.         if #currLine + #words[i] + 1 > width then
  688.             table.insert( t, currLine )
  689.             currLine = words[i].." "
  690.         else
  691.             currLine = currLine..words[i].." "
  692.         end
  693.        
  694.         if i == #words then table.insert( t, currLine ) end
  695.     end
  696.    
  697.     return t
  698. end
  699.  
  700. function splitLine( str, width )
  701.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  702.     if type( width ) ~= "number" then error( "Bad argument: Number expected, got "..type( width ), 2 ) end
  703.     if width < 1 then error( "Bad argument: Width must be positive", 2 ) end
  704.     return table.concat( splitLineToTable( str, width ), "\n" )
  705. end
  706.  
  707. function count( str, regex )
  708.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  709.     if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
  710.     local count = 0
  711.     local fpat = "(.-)"..regex
  712.     local last_end = 1
  713.     local s, e, cap = str:find(fpat, last_end)
  714.     while s do
  715.         count = count + 1
  716.         last_end = e+1
  717.         s, e, cap = str:find(fpat, last_end)
  718.     end
  719.     return count
  720. end
  721.  
  722. function countIgnoreCase( str, regex )
  723.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  724.     if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
  725.     str = str:lower()
  726.     local count = 0
  727.     local fpat = "(.-)"..regex:lower()
  728.     local last_end = 1
  729.     local s, e, cap = str:find(fpat, last_end)
  730.     while s do
  731.         count = count + 1
  732.         last_end = e+1
  733.         s, e, cap = str:find(fpat, last_end)
  734.     end
  735.     return count
  736. end
  737.  
  738. function isLower( str )
  739.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  740.     return (str:find("%L")) == nil
  741. end
  742.  
  743. function isUpper( str )
  744.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  745.     return (str:find("%U")) == nil
  746. end
  747.  
  748. function isAlpha( str )
  749.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  750.     return (str:find("%A")) == nil
  751. end
  752.  
  753. function isAlphaNumeric( str )
  754.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  755.     return (str:find("%W")) == nil
  756. end
  757.  
  758. function isNumeric( str )
  759.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  760.     return (str:find("%D")) == nil
  761. end
  762.  
  763. function isPunctuation( str )
  764.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  765.     return (str:find("%P")) == nil
  766. end
  767.  
  768. function isHexadecimal( str )
  769.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  770.     return (str:find("%X")) == nil
  771. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement