Advertisement
fishermedders

split

Oct 22nd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. local function splitWords(Lines, limit)
  2. while #Lines[#Lines] > limit do
  3. Lines[#Lines+1] = Lines[#Lines]:sub(limit+1)
  4. Lines[#Lines-1] = Lines[#Lines-1]:sub(1,limit)
  5. end
  6. end
  7.  
  8. local function wrap(str, limit)
  9. local Lines, here, limit, found = {}, 1, limit or 72, str:find("(%s+)()(%S+)()")
  10.  
  11. if found then
  12. Lines[1] = string.sub(str,1,found-1) -- Put the first word of the string in the first index of the table.
  13. else Lines[1] = str end
  14.  
  15. str:gsub("(%s+)()(%S+)()",
  16. function(sp, st, word, fi) -- Function gets called once for every space found.
  17. splitWords(Lines, limit)
  18.  
  19. if fi-here > limit then
  20. here = st
  21. Lines[#Lines+1] = word -- If at the end of a line, start a new table index...
  22. else Lines[#Lines] = Lines[#Lines].." "..word end -- ... otherwise add to the current table index.
  23. end)
  24.  
  25. splitWords(Lines, limit)
  26.  
  27. return Lines
  28. end
  29.  
  30. local myText = "THIS IS A REALLY LONG TEXT THAT I WOULD LIKE TO WRAP AT A CERTAIN MARGIN."
  31.  
  32. local myTable = wrap(myText,20)
  33.  
  34. for k,v in pairs(myTable) do print(k.." = "..v) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement