Nasal library/string: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(http://forum.flightgear.org/viewtopic.php?f=30&t=27398)
 
(functions of "Nasal\strings.nas" added by intelligent copy/paste)
Line 1: Line 1:
{{WIP}}
{{Stub}}
{{Nasal Navigation}}
{{Nasal Navigation}}
== 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|Nasal library]] page.
=== string.isalnum() ===
<syntaxhighlight lang="nasal">
string.isalnum();
</syntaxhighlight>
=== string.isalpha() ===
<syntaxhighlight lang="nasal">
string.isalpha();
</syntaxhighlight>
=== string.isascii() ===
<syntaxhighlight lang="nasal">
string.isascii();
</syntaxhighlight>
=== string.isblank() ===
<syntaxhighlight lang="nasal">
string.isblank();
</syntaxhighlight>
=== string.iscntrl() ===
<syntaxhighlight lang="nasal">
string.iscntrl();
</syntaxhighlight>
=== string.isdigit() ===
<syntaxhighlight lang="nasal">
string.isdigit();
</syntaxhighlight>
=== string.isgraph() ===
<syntaxhighlight lang="nasal">
string.isgraph();
</syntaxhighlight>
=== string.islower() ===
<syntaxhighlight lang="nasal">
string.islower();
</syntaxhighlight>
=== string.isprint() ===
<syntaxhighlight lang="nasal">
string.isprint();
</syntaxhighlight>
=== string.ispunct() ===
<syntaxhighlight lang="nasal">
string.ispunct();
</syntaxhighlight>
=== string.isspace() ===
<syntaxhighlight lang="nasal">
string.isspace();
</syntaxhighlight>
=== string.isupper() ===
<syntaxhighlight lang="nasal">
string.isupper();
</syntaxhighlight>
=== string.isxdigit() ===
<syntaxhighlight lang="nasal">
string.isxdigit();
</syntaxhighlight>
=== string.isxspace() ===
<syntaxhighlight lang="nasal">
string.isxspace();
</syntaxhighlight>
=== string.toupper() ===
<syntaxhighlight lang="nasal">
string.toupper();
</syntaxhighlight>
=== string.tolower() ===
<syntaxhighlight lang="nasal">
string.tolower();
</syntaxhighlight>
=== 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:
<syntaxhighlight lang="nasal">
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
</syntaxhighlight>
=== string.lc() ===
Return string converted to lower case letters.
<syntaxhighlight lang="nasal">
string.lc();
</syntaxhighlight>
=== string.uc() ===
Return string converted to upper case letters.
<syntaxhighlight lang="nasal">
string.uc();
</syntaxhighlight>
=== string.icmp() ===
Case insensitive string compare function.
<syntaxhighlight lang="nasal">
string.icmp();
</syntaxhighlight>
=== string.imatch() ===
Case insensitive match function.
<syntaxhighlight lang="nasal">
string.imatch();
</syntaxhighlight>
=== string.match(str, patt) ===
Check if string <str> matches shell style pattern <patt>
<syntaxhighlight lang="nasal">
# 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"
</syntaxhighlight>
=== 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.                                               
<syntaxhighlight lang="nasal">
string.normpath();
</syntaxhighlight>
=== string.join(sep,list) ===
Join all elements of a list inserting a separator between every two of them.
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
string.join(sep,list);
</syntaxhighlight>
=== string.replace(str,old,new) ===
Replace all occurrences of 'old' by 'new'.
<syntaxhighlight lang="nasal">
string.replace(str,old,new);
</syntaxhighlight>
=== 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               
<syntaxhighlight lang="nasal">
var r = string.scanf("comm3freq123.456", "comm%ufreq%f", var result = []);   
</syntaxhighlight>
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.
<syntaxhighlight lang="nasal">
string.compileTemplate(template);
</syntaxhighlight>
Example (Build MapQuest tile url):                               
<syntaxhighlight lang="nasal">
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
</syntaxhighlight>
</syntaxhighlight>

Revision as of 09:35, 21 May 2016

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