Commit Graph

30 Commits

Author SHA1 Message Date
Max Cahill
9a8493b880 added stringx.split_and_trim to save me inlining something with the same effect all over the place 2022-10-24 16:08:39 +11:00
Max Cahill
7285531870 fixed dedent() with trimmed blank lines (some editors do this automatically) 2022-09-20 15:00:44 +10:00
Max Cahill
24fde1c157 made stringx.deindent remove all trailing empties rather than just one 2022-07-11 16:44:51 +10:00
David Briscoe
d6af9f74c5 lint: Don't set unused locals
Not a bug, but pointless to store this variable since it's not used.
2022-03-03 10:17:35 -08:00
David Briscoe
66a6c5a50e Add comments for things that surprised me
As a new user, there were things I was skeptical about and after digging
in, these were my conclusions.

Compared to the simple and obvious lua wiki solutions, batteries' string
functions are slightly faster. GC is the same.

Test
    local str = "hello world"
    local fn = function()
        local x = 0
        if stringx.ends_with(str, "h") then
            x = x + 1
        end
        if stringx.ends_with(str, "helll") then
            x = x + 1
        end
        if stringx.ends_with(str, "helicopter") then
            x = x + 1
        end
    end
    local pretty = require "inspect"
    print("stringx =", pretty({
                time_taken = {measure.time_taken(fn, 10000)},
                memory_taken = {measure.memory_taken(fn, 10000)}
        }))
    local function starts_with(str, prefix)
        return str:find(prefix, 1, true) == 1
    end
    local function ends_with(str, ending)
        return ending == "" or str:sub(-#ending) == ending
    end
    local fn = function()
        local x = 0
        if ends_with(str, "h") then
            x = x + 1
        end
        if ends_with(str, "helll") then
            x = x + 1
        end
        if ends_with(str, "helicopter") then
            x = x + 1
        end
    end
    print("find =", pretty({
                time_taken = {measure.time_taken(fn, 10000)},
                memory_taken = {measure.memory_taken(fn, 10000)}
        }))

starts_with
===========

stringx =       {
  memory_taken = { 0, 0, 0 },
  time_taken = { 1.5098012518138e-007, 9.988434612751e-008, 2.1699932403862e-005 }
}
find =  {
  memory_taken = { 0, 0, 0 },
  time_taken = { 2.7349997544661e-007, 1.9988510757685e-007, 9.1999536380172e-006 }
}

ends_with
=========

stringx =       {
  memory_taken = { 0, 0, 0 },
  time_taken = { 9.0479978825897e-008, 0, 2.5199959054589e-005 }
}
find =  {
  memory_taken = { 0, 0, 0 },
  time_taken = { 2.1833006758243e-007, 1.9988510757685e-007, 6.1000464484096e-006 }
}
2022-03-03 10:17:34 -08:00
TurtleP
2ef834d538 max_split -> limit, allow zero limit 2021-12-26 11:32:43 -05:00
Jeremy S. Postelnek
e5d00ef3a7
clarify the non-zero *and* positive error 2021-12-24 13:23:37 -05:00
Jeremy S. Postelnek
b01ba8e506
fix spacing 2021-12-24 13:19:38 -05:00
Jeremy S. Postelnek
23bc392b6e
check for negative max_split value 2021-12-24 13:19:12 -05:00
Jeremy S. Postelnek
660ae8ca52
add max_split to stringx.split 2021-12-24 13:03:36 -05:00
Max Cahill
76ed4b1a02 stringx.apply_template works with $snake_case_symbols 2021-07-13 16:47:19 +10:00
Max Cahill
20aa83f79e added stringx.contains 2021-07-06 08:29:30 +10:00
Max Cahill
77d954e58a stringx.split delimiter optional 2021-07-06 08:29:13 +10:00
Max Cahill
e726f19a5e added pretty module with pretty.print and pretty.string
breaking: string.pretty still exists but takes new config argument
2021-07-05 16:12:16 +10:00
Max Cahill
62957a9270 [modified] PR #17 a little; simplified various unneeded sections and added comments 2021-04-14 17:06:31 +10:00
Alvaro Frias
dbadd9af83 Refactored rtrim and ltrim to use _whitespace_bytes table 2021-04-12 12:38:03 -03:00
Alvaro Frias Garay
6042e3a705 Renamed variable properly 2021-04-07 14:09:40 -03:00
Alvaro Frias Garay
fb4c187347 Added ends_with method to stringx 2021-04-07 13:59:42 -03:00
Alvaro Frias Garay
e8bb432999 Added ltrim and rtrim methods
complementary methods to trim
2021-04-05 23:43:35 -03:00
Max Cahill
290b39236c [modified] stringx.starts_with to not generate garbage 2021-03-02 11:47:28 +11:00
Max Cahill
dfd1a1c69f [added] stringx.starts_with 2021-03-02 11:45:19 +11:00
Max Cahill
8be736d992 [added] stringx.apply_template for "$template_var" containing strings 2020-11-19 17:18:38 +11:00
Max Cahill
01ffdc09cd [added] stringx.trim 2020-11-12 16:36:06 +11:00
Max Cahill
baa71bdf4e [added] stringx.deindent (and alias dedent) 2020-11-12 14:32:56 +11:00
Max Cahill
8c5fa157c7 [dev] note about strings with spaces being a potential problem with pretty printer; they arent re-parse friendly at the moment 2020-07-22 20:51:54 +10:00
Max Cahill
04563ed4b7 [modified] stringx.pretty handles embedded tables better (still likely needs a wrap width instead of an element count per line) 2020-06-19 15:17:24 +10:00
Max Cahill
95a9552cde [added] wip multi-line support to stringx.pretty 2020-05-27 13:03:40 +10:00
Max Cahill
284856d2c3 [modified] refactored init.lua extensively; used new assert module in stringx and tablex 2020-05-19 12:03:45 +10:00
Max Cahill
e75147ec77 [modified] moved tablex.stringify to stringx.pretty 2020-04-17 10:45:15 +10:00
Max Cahill
26acf1752d [added] stringx module (just split for now) and overlay to string on export 2020-04-17 10:35:00 +10:00