acid.strutil
This library is considered production ready.
It provides with several often used string operation utilities. Most of them follow python-style.
local strutil = require("acid.strutil")
strutil.split('a/b/c', '/') -- {'a', 'b', 'c'}
-- convert series of data into human readable string.
strutil.to_str(1,2,{10,a=1,20}) -- 12{10,20,a=1}
syntax:
strutil.endswith(str, suffix)
Return true
if str
ends with the specified suffix
,
false
otherwise.
See also: strutil.startswith
.
arguments:
-
str
: is a string. -
suffix
: suffix string or a table of strings to try.
return: bool
syntax:
strutil.fnmatch(s, ptn)
Return true
if string s
matches the string ptn
,
false
otherwise.
arguments:
-
s
: is a string. -
ptn
: is used to find the first match ofptn
in the strings
.Convert escape and special chars in
ptn
to chars that Lua can recognize.
return: bool.
syntax:
strutil.fromhex(str)
Convert '00ab'
to '\x00\xab'
.
See also: strutil.tohex
arguments:
str
: hex string.
return: byte string.
error:
An error will be emitted if str
is not a string, or it is not a valid hex.
syntax:
strutil.ljust(str, n, char)
Return str
left-justified in a string of length n
.
Padding is done using the specified fill character.
By default char
is space.
See also strutil.rjust
.
arguments:
-
str
: string to justify. -
n
: specifies result string length. -
char
: is filling char.
return: a padded string.
syntax:
strutil.join(sep, ...)
Same as table.concat({...}, sep)
arguments:
sep
: Separator string.
return: a string.
syntax:
strutil.placeholder(val, pholder, float_fmt)
Return a string representing val
or a placeholder string if val
is nil
or ''
.
arguments:
-
val
: any type of value. -
pholder
: specifies what string to use as a place holder.By default it is
'-'
. -
float_fmt
: specifies float number format to convert to string.By default it is
nil
. Ifval
is float, it is converted to string withtostring(val)
.
return: string.
syntax:
strutil.right_n_split(str, pat, frm, plain, n)
Split string str
from the right with a separator pat
.
Return two values:
The offset of the end of the part that is not split in str
,
and following n
parts in a table in reversed order.
For example:
strutil.right_n_split('a/b/c/d', '/', 1, true, 2) -- 3, {'d', 'c'}
arguments:
-
str
: is the string to be split. -
pat
: is a separator in string. -
frm
: specifies the starting position to find thepat
. -
plain
: turns off the pattern matching facilities. -
n
: limits max split times.
return:
two values,
a number and a table of n
split strings.
syntax:
strutil.rjust(str, n, char)
Return str
right-justified in a string of length n
.
Padding is done using the specified fill character.
By default char
is space.
See also strutil.ljust
.
arguments:
-
str
: string to justify. -
n
: specifies result string length. -
char
: is filling char.
return: a padded string.
syntax:
strutil.rsplit(str, pattern, opts)
Same as strutil.split
except when maxsplit
is specified,
it starts with splitting from right to left.
syntax:
strutil.split(str, pattern, opts)
Split string str
with a separator pattern
strutil.split('a/b/c/d', '/', 2) -- {'a', 'b', 'c/d'}
arguments:
-
str
: is the string to be split. -
pattern
: is a separator in lua string pattern or a plain text string, depending on the third argument. -
opts
: is an option to control the behavior of this function.The value of
opts
could be:-
nil
: lua string pattern.strutil.split(str, pattern)
It splits
str
with lua string patternpattern
. -
true
: pattern is plain text.strutil.split(str, pattern, true)
It splits
str
with plain text separatorpattern
. -
number: use plain text pattern and it limits max split times.
strutil.split(str, pattern, 3)
.It splits
str
with plain textpattern
and splits at most 3 times. -
table: options in a table. Valid option keys are
plain
andmaxsplit
.strutil.split(str, pattern, {plain=false, maxsplit=5})
It splits
str
with lua string pattern and splits at most 5 times.
-
return: a table of split strings.
syntax:
strutil.startswith(str, prefix, start)
Return true
if str
starts with the specified prefix
,
false
otherwise.
See also: strutil.endswith
.
arguments:
-
str
: is a string. -
prefix
: prefix string or a table of strings to try. -
start
: is the position to start test.By default it is
nil
: to test from the first char.
return: bool
syntax:
strutil.contains(str, sub_str, opts)
Return true
if str
contains sub_str
,
false
otherwise.
arguments:
-
str
: is a string. -
sub_str
: sub string or a table of sub strings to try. -
opts
: is a table contains any of the following fields.-
logic
: whensub_str
is a table, setlogic
to 'and' means that only returntrue
ifstr
contains all items in tablesub_str
, set to 'or' means that returntrue
ifstr
contains any item in tablesub_str
. Ifsub_str
is not a table, value oflogic
is ignored. -
start_index
: is the position to start test.By default it is 1: to test from the first char.
-
end_index
: is the position to end test.By default it is the length of
str
.
-
return: bool
syntax:
strutil.strip(str, ptn)
Return a string with leading and trailing chars those matches pth
removed.
arguments:
-
str
: string. -
ptn
: specifies chars in plain text to remove from both side of thestr
.if
ptn
isnil
or empty string""
, it removes all space chars(" ", "\t", "\r" and "\n"
).
return:
a string with pth
removed from both side.
syntax:
strutil.to_chunks(s, n)
Divide a string s
into sections by length n
.
arguments:
-
s
: is a string. -
n
: specifies the length of each part.It must be greater than 0, and it can be a float.
when it is a float, for Example:
strutil.to_chunks('abcdefgh', 1.34) -- {'a', 'b', 'cd', 'e', 'f', 'gh'} strutil.to_chunks('abcdef', 1.5) -- {'a', 'bc', 'd', 'ef'}
return: a table of split strings.
syntax:
strutil.to_str(...)
Convert all arguments to a human readable string. Example:
strutil.to_str(1,2,{10,a=1,20}) -- 12{10,20,a=1}
It is actually just a wrapper of underlying repr.str()
.
arguments:
...
: a series of element of typenumber, string, bool, table or nil
.
return: a human readable string
syntax:
strutil.tohex(str)
Convert '\x00\xab'
to '00ab'
.
See also: strutil.fromhex
arguments:
str
: any string.
return: hex string.
error:
An error will be emit if str
is not a string, or it is not a valid hex.
Zhang Yanpo (张炎泼) [email protected]
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) [email protected]