Advertisement
Shiranuit

stringBuilder

May 4th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. local oldType = type
  2. type = function(val)
  3.   local ot = oldType(val)
  4.     if ot=="table" then
  5.     local meta = getmetatable(val)
  6.     return (meta and meta.__type) or "table"
  7.   end
  8.   return ot
  9. end
  10.  
  11. -- StringBuilder
  12. local stringbuilderFunctions = {}
  13.  
  14. local function getTexte(stringb)
  15.     if type(stringb) == "stringBuilder" then
  16.         if getmetatable(stringb) and getmetatable(stringb).__txt then
  17.             return getmetatable(stringb).__txt
  18.         else
  19.             return ""
  20.         end
  21.     else
  22.         return tostring(stringb)
  23.     end
  24. end
  25.  
  26. stringbuilderFunctions.split = function(self, sep, _useregex)
  27.   local ret = {}
  28.   local last = 1
  29.   local fn = string.find(getTexte(self), getTexte(sep), 1, not _useregex)
  30.   while fn do
  31.     ret[#ret+1] = string.sub(getTexte(self), last, fn-1)
  32.     last = fn + #getTexte(sep)
  33.     fn = string.find(getTexte(self), getTexte(sep), last, not _useregex)
  34.   end
  35.   ret[#ret+1] = string.sub(getTexte(self), last)
  36.   return ret
  37. end
  38.  
  39. stringbuilderFunctions.contains = function(self, fTexte, _useregex)
  40.       return string.find(getTexte(self), getTexte(fTexte), 1, not _useregex) ~= nil
  41. end
  42.  
  43. stringbuilderFunctions.endWith = function(self, fTexte)
  44.       return fTexte == string.sub(getTexte(self), -#getTexte(fTexte),-1)
  45. end
  46.  
  47. stringbuilderFunctions.startWith = function(self, fTexte)
  48.       return fTexte == string.sub(getTexte(self), 1, #getTexte(fTexte))
  49. end
  50.  
  51. stringbuilderFunctions.left = function(self, longueur)
  52.     return stringB.new(string.sub(getTexte(self), 1, longueur))
  53. end
  54.  
  55. stringbuilderFunctions.lTrim = function(self)
  56.     return stringB.new(string.match(getTexte(self),"([^ ][ ]*.+)"))
  57. end
  58.  
  59. stringbuilderFunctions.rTrim = function(self)
  60.     return stringB.new(string.match(getTexte(self),"(.+[ ]*[^ ])"))
  61. end
  62.  
  63. stringbuilderFunctions.right = function(self, longueur)
  64.     return stringB.new(string.sub(getTexte(self), #getTexte(self)-longueur, #getTexte(self)))
  65. end
  66.  
  67. stringbuilderFunctions.mid = function(self, debut, fin)
  68.     if not fin then
  69.         return stringB.new(string.sub(getTexte(self), debut))
  70.     else
  71.         return stringB.new(string.sub(getTexte(self), debut, fin))
  72.     end
  73. end
  74.  
  75. stringbuilderFunctions.space = function(self, num)
  76.     if num and num > 0 then
  77.         return stringB.new(getTexte(self)..string.rep(" ",num))
  78.   else
  79.     return stringB.new(getTexte(self))
  80.   end
  81. end
  82.  
  83. stringbuilderFunctions.hash = function(self,number)
  84.     local num = type(number)=="number" and number or 1
  85.     local tTable = {}
  86.     for i=1, #getTexte(self), num do
  87.         tTable[#tTable+1]=string.sub(getTexte(self),i,i+num-1)
  88.     end
  89.     return tTable
  90. end
  91.  
  92. stringbuilderFunctions.complete = function(self,dictionnaire)
  93.   if dictionnaire == nil then
  94.     return nil
  95.   else
  96.     if getTexte(self) == "" then
  97.       return nil
  98.     else
  99.       tTable = {}
  100.       for k, v in pairs(dictionnaire) do
  101.         if string.sub(v,1,#getTexte(self)) == getTexte(self) then
  102.           tTable[#tTable+1]=string.sub(v,#getTexte(self)+1,#v)
  103.         end
  104.       end
  105.       return tTable
  106.     end
  107.   end
  108. end
  109.  
  110. stringbuilderFunctions.rep = function(self, num)
  111.     return stringB.new(string.rep(getTexte(self),num))
  112. end
  113.  
  114. stringbuilderFunctions.inv = function(self)
  115.     return stringB.new(string.reverse(getTexte(self)))
  116. end
  117.  
  118. stringbuilderFunctions.add = function(self, _stringbuilder)
  119.   return stringB.new(getTexte(self)..getTexte(_stringbuilder))
  120. end
  121.  
  122. stringbuilder = {}
  123. for k,v in pairs(stringbuilderFunctions) do stringbuilder[k]=v end
  124.  
  125. stringbuilder.new = function(txt)
  126.   txt = getTexte(txt)
  127.     return setmetatable({},{
  128.     __index=stringbuilderFunctions,
  129.     __add=stringbuilderFunctions.add,
  130.     __mul=stringbuilderFunctions.rep,
  131.     __div=stringbuilderFunctions.hash,
  132.     __concat=stringbuilderFunctions.add,
  133.     __tostring=function(tble) return getmetatable(tble).__txt end,
  134.     __txt=txt,
  135.     __type="stringBuilder"
  136.   })
  137. end
  138.  
  139. stringB = stringbuilder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement