Library globals

Namespace globals . string

View source

(no description)

Classes

Scan

Simple scanf function. Takes an input string, a pattern, and a vector. It returns 0 if the format didn't match, and appends all found elements to the given vector. Return values: -1 string matched format ending with % (i.e. more chars than format cared about) 0 string didn't match format 1 string matched, but would still match if the right chars were added 2 string matched, and would not if any character would be added var r = string.scanf("comm3freq123.456", "comm%ufreq%f", var result = []); The result vector will be set to [3, 123.456].

Functions

color

compileTemplate

Get a function out of a string template for fast insertion of template parameters. This allows to use the same templates as with most available tile mapping engines (eg. Leaflet, Polymaps). Return a callable function object on success, and nil if parsing the templated fails. See string._template_getargs for more on calling a compile object. Example (Build MapQuest tile url): var makeUrl = string.compileTemplate( "http://otile1.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg" ); print( makeUrl({x: 5, y: 4, z: 3}) ); Output: http://otile1.mqcdn.com/tiles/1.0.0/map/3/5/4.jpg

icmp

case insensitive string compare and match functions (not very efficient -- converting the array to be sorted first is faster)

imatch

isalnum

isalpha

isascii

isblank

iscntrl

isdigit

isgraph

islower

isprint

ispunct

isspace

isupper

isxdigit

isxspace

join

Join all elements of a list inserting a separator between every two of them.

lc

return string converted to lower case letters

match

check if string <str> matches shell style pattern <patt> Rules: ? stands for any single character * stands for any number (including zero) of arbitrary characters \ escapes the next character and makes it stand for itself; that is: \? stands for a question mark (not the "any single character" placeholder) [] stands for a group of characters: [abc] stands for letters a, b or c [^abc] stands for any character but a, b, and c (^ as first character -> inversion) [1-4] stands for digits 1 to 4 (1, 2, 3, 4) [1-4-] stands for digits 1 to 4, and the minus [-1-4] same as above [1-3-6] stands for digits 1 to 3, minus, and 6 [1-3-6-9] stands for digits 1 to 3, minus, and 6 to 9 [][] stands for the closing and the opening bracket (']' must be first!) [^^] stands for all characters but the caret symbol [\/] stands for a backslash or a slash (the backslash isn't an escape character in a [] character group) Note that a minus can't be a range delimiter, as in [a--e], which would be interpreted as any of a, e, or minus. Example: string.match(name, "*[0-9].xml"); ... true if 'name' ends with digit followed by ".xml"

normpath

Removes superfluous slashes, empty and "." elements, expands all ".." elements keeping relative paths, and turns all backslashes into slashes. The result will start with a slash if it started with a slash or backslash, it will end without slash.

replace

Replace all occurrences of 'old' by 'new'.

scanf

setcolors

ANSI colors (see $ man console_codes)

tolower

toupper

trim

trim spaces at the left (lr < 0), at the right (lr > 0), or both (lr = 0) An optional function argument defines which characters should be trimmed: string.trim(a); # trim spaces string.trim(a, 1, string.isdigit); # trim digits at the right string.trim(a, 0, func(c) c == `\\` or c == `/`); # trim slashes/backslashes

truncateAt

truncate at the first match string.truncateAt("file.xml", ".xml"); # "file.xml" -> "file" string.truncateAt("file.xml", ".txt"); # "file.xml" -> "file.xml"

uc

return string converted to upper case letters

Variables

color_enabled

Add ANSI color codes to string, if terminal-ansi-colors are enabled and stderr prints to a terminal. Example: print(string.color("31;1", "this is red"));