Nasal library/string

From FlightGear wiki
Revision as of 09:35, 21 May 2016 by Mhab (talk | contribs) (functions of "Nasal\strings.nas" added by intelligent copy/paste)
Jump to navigation Jump to search
This article is a stub. You can help the wiki by expanding it.

String Handling

This page contains description of functions available in "$FG_ROOT\data\Nasal\strings.nas". For string related functions in Nasal also see Nasal library page.

string.isalnum()

string.isalnum();

string.isalpha()

string.isalpha();

string.isascii()

string.isascii();

string.isblank()

string.isblank();

string.iscntrl()

string.iscntrl();

string.isdigit()

string.isdigit();

string.isgraph()

string.isgraph();

string.islower()

string.islower();

string.isprint()

string.isprint();

string.ispunct()

string.ispunct();

string.isspace()

string.isspace();

string.isupper()

string.isupper();

string.isxdigit()

string.isxdigit();

string.isxspace()

string.isxspace();

string.toupper()

string.toupper();

string.tolower()

string.tolower();

string.trim(str,lr[,func])

Trim spaces at the left (lr < 0), at the right (lr > 0), or both (lr = 0) is default. 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

string.lc()

Return string converted to lower case letters.

string.lc();

string.uc()

Return string converted to upper case letters.

string.uc();

string.icmp()

Case insensitive string compare function.

string.icmp();

string.imatch()

Case insensitive match function.

string.imatch();

string.match(str, patt)

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"

string.normpath(path)

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.

string.normpath();

string.join(sep,list)

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

string.join(sep,list);

string.replace(str,old,new)

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

string.replace(str,old,new);

string.scanf(test,format,result)

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].

string.compileTemplate(template,type=nil)

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.

string.compileTemplate(template);

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