Types
String type
- Eachable1
-
Eachable which each() calls the callback with one argument
Constructors
Str(t:Type)
String representation of a type
Str(i:NormalTypeInstance)
String representation of normal type instance i
Normal type is a user defined type. In addition some types defined by NGS are also normal types.
Str(r:Range)
Convert range to human readable representation
Str(x:Any, target_width:Int)
Convert anything to Str of a given width
Str(s:Str, target_width:Int)
Pad a string to given width with spaces
Str(n:Int, target_width:Int)
Convert a number to a string and pad it
Str(p:Path)
String representation of a Path (or it's sub-type)
Str(f:File)
String representation of File
Str(p:Process)
Wait for the process to finish and return its standard output.
Methods
+(s:Str, a:Arr)
Prepend each line in a with s
+(a:Arr, s:Str)
Append s to each line in a
-(s:Str, pfx:Pfx)
Return string without the prefix. Throws MatchFail if pfx is MustPfx but s does not start with it.
-(s:Str, sfx:Sfx)
Return string without the suffix. Throws MatchFail if pfx is MustSfx but s does not end with it.
-(s:Str, r:RegExp)
Returns the string with one occurrence of regexp cut out of it. Throws InvalidArgument if s does not contain r.
.(regexp:RegExp, attr:Str)
Get attributes of a RegExp. Throws AttrNotFound if attr is not one of the allowed values. You should not use this directly. Use "~" and "~~" operators.
.(obj:BasicType, attr:Str)
Get BasicType (Int, Arr, Hash, ...) attribute. Throws AttrNotFound.
.(obj:NormalType, attr:Str)
Get NormalType (a type that is typically defined by user) attribute. Throws AttrNotFound.
.(obj:NormalTypeInstance, attr:Str)
Get NormalType (a type that is typically defined by user) instance attribute. Throws AttrNotFound.
.(a:Arr, attr:Str)
Return array made of given attribute of each element of given array
.=(obj:NormalTypeInstance, attr:Str, v:Any)
Set Normal type (a type that is typically defined by user) instance attribute. Throws AttrNotFound.
/(a:Str, b:Str)
Concatenate two path parts using STDLIB_PATH_SEP (currently "/") but should be platform-specific later.
/(a:Str, b:Str)
Concatenate two path parts using STDLIB_PATH_SEP (currently "/") but should be platform-specific later.
/(a:Str, b:Str)
Concatenate two path parts using STDLIB_PATH_SEP (currently "/") but should be platform-specific later.
//(regexp:Str, flags:Str)
Regular expression constructor. Called by NGS when /myregex/ syntax is used.
<=(a:Str, b:Str)
Case sensitive LessThan or Equal comparison for strings
[](lib:CLib, symbol:Str)
Unfinished feature. Don't use!
[](s:Str, i:Int)
Get given character (currently byte) of the string
[](s:Str, i:Int)
Get given character (currently byte) of the string.
TODO: Document InvalidArgument exception.
[](s:Str, r:NumRange)
Get a substring. Indexes in s are specified by NumRange.
[](s:Str, r:RegExp)
Get substring of a string that corresponds to first match of given regular expression
%EX "x10ab20c30y"[/[0-9]+/] # "10"
assert(condition:Bool, msg:Str)
Throws AssertFail with givens message if condition is false
basename(s:Str)
Get basename of the file. Uses BASENAME(1).
c_pcre_exec(regexp:RegExp, subject:Str, offset:Int, options:Int)
Search string for regular expression. Uses PCRE_EXEC(3). Do not use this function directly!
code(s:Str)
Convert a Str to NGS code that would produce the string when executed. Not fully functional yet.
BUG: Does not do escaping.
config(k:Str)
Get configuration for the given key. Lookup order / first wins: (1) Environment variable NGS_$k (2) previously set config(k, v)
config(k:Str, v:Any)
Set configuration. To be used with config(k:Str).
debug(s:Str)
Same as debug('default', s)
debug(facility:Str, s:Str)
Debug to standard error.
"DEBUG" environment variable must be non-empty string to activate. Otherwise nothing is outputted.
TODO: Not output thread ID if there is only one thread.
TODO: Timestamps?
die(s:Str, exit_code:Any=1)
Write message in s to standard error and exit
dir(dirname:Str, subtype:Any=false)
List directory contents. Warning: "." and ".." are included.
Throws DirFail when directory can not be listed.
dir(dirname:Str, cb:Fun, subtype:Any=false)
List directory contents and call cb with Path() of each found item. Warning: "." and ".." are included.
each(s:Str, cb:Fun)
Iterates over all string characters (currently bytes).
TODO: example.
echo(s:Str)
Print given string and a newline to stdout.
echo(fd:Int, s:Str)
Print given string and a newline to a file referenced by descriptor.
encode_hex(s:Str)
Encodes each character in a string as two uppercase hexadecimal digits
ends_with(haystack:Str, needle:Str)
Check whether a string ends with another string
error(s:Str)
Write error message to standard error.
fetch(filename:Str, parse_hints:Hash={})
read() and parse() the given file
find_in_path(executable_name:Str)
Finds given binary and returns it's full path.
Throws ExecutableNotFound if the binary was not found.
Search strategy: "/" in the name of the binary means that given executable_name is a path so it's returned as-is (wrapped in Path).
If PATH environment variable exists, the given directories are searched.
If PATH is not set built-in value for PATH is used to search: /usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin
get(e:Eachable1, attr:Str)
Return array made of given attribute of each element of given Eachable1 where present
get(e:Eachable1, attr:Str, dflt:Any)
Return array made of given attribute of each element of given Eachable1 where present or default value where the attribute is not present
Return array (? depends on map() ?) made of given attribute of each element of given Eachable1
glob(pattern:Str, start_dir:Any=.)
Work in progress, do not use!
global_not_found_handler(name:Str)
Called when reading undefined global.
Implements autoloading.
Searches in $NGS_DIR/autoload/NAME.ngs
WARNING: May have security implications when looking up a name from untrusted source.
Hash(arr:Arr, attr:Str)
TODO. Not sure it's used anywhere. Maybe remove?
Hash(arr:Arr, key_attr:Str, val_attr:Str)
Create Hash from Arr of something that has key and value attribute
in(symbol:Str, lib:CLib)
Unfinished feature. Don't use!
in(attr:Str, obj:NormalTypeInstance)
Check whether NormalType (a type that is typically defined by user) instance has an attribute.
in(needle:Str, haystack:Str)
Determin if needle substring occurs at least once in haystack
init(e:CException, message:Str)
CException constructor. In addition to storing message attribute, adds errno and errno_name attributes.
Int(s:Str)
Convert base-10 string to Int.
Throws InvalidArgument if the number in s is not well-formatted.
join(a:Arr, s:Str)
Join non-strings. Converts a elements to string first, then uses built-in join().
limit(s:Str, n:Int, marker:Str=)
Truncate a string if necessary so it would have maximum n characters (currently bytes).
lines(s:Str)
Split s to strings using end-of-line separators.
UNIX and Windows line endings supported (Windows - not tested yet).
Warning: Max OS <= 9 line separation of CR (\r) is not supported
lines(s:Str, cb:Fun)
Split s to strings using end-of-line separators and call cb for each one of the lines.
TODO: More efficient implementation, which would not have temporary array of all lines.
log(s:Str)
Log to standard output. Later log output will be treated specially by the shell. It will have suitable representation in the UI. Use log() when it's semantically a log.
lstat(pathname:Str)
Issue lstat() system call. Throws StatFail if the call fails.
lte(a:Str, b:Str)
Case-insensitive LessThan or Equal comparison for strings
open(f:File, flags:Str)
Open a file and set the "fd" field to the file descriptor. Uses OPEN(2).
Throws InvalidArgument if file is already open.
Throws FileIOFail if an underlying error occurs.
opt_prop(rd:ResDef, name:Str, props:Hash)
Get optional resource property, looking up in properties first and then in anchor
opt_prop(rd:ResDef, name:Str, props:Hash, cb:Fun)
Run cb with optional resource property if it exists, uses opt_prop(ResDef, Str, Hash)
ord(s:Str, idx:Int)
Get character (byte) ordinal value. Throws InvalidArgument if idx is not pointing into s.
ord(s:Str)
Get character (currently byte) code. Throws InvalidArgument if s is not of length 1.
parse(s:Str, hints:Hash)
Attempt to parse JSON. Uses decode_json().
parse(s:Str, hints:Hash)
EXPERIMENTAL. KVS (key-value separator) hint for parse()
parse(s:Str, hints:Hash)
EXPERIMENTAL. FS (field separator) hint for parse()
parse(s:Str, hints:Hash)
Handle fields_names hint - run parse() and make Arr[Hash] from Arr[Arr] using provided fields_names
parse(s:Str, hints:Hash)
Parse the output of "aws" command. Extracts the array (see "s" below"). For "describe-instances", flattens the instance list.
parse(s:Str, hints:Hash)
Parse the output of "find" command which does not use "-printf". Handles "-print0".
pos(haystack:Str, needle:Str)
Find substring in a string.
rand(a:Str)
Pick random character from a string
Real(s:Str)
Convert a string to real (floating) number, inefficiently
req_prop(rd:ResDef, name:Str, props:Hash)
Get resource property, looking up in properties first and then in anchor
set(obj:Any, attr:Str, val:Any)
Sets the given attribute to the given value
sort(a:Arr, attr:Str, lte:Fun=(a function))
Sort an array based on attribute value
split(s:Str, r:RegExp)
%EX "x10ab20c30y".split(/[0-9]+/).join(" :: ") # "x :: ab :: c :: y"
starts_with(haystack:Str, needle:Str)
Check whether a string starts with another string
stat(pathname:Str)
Issue stat() system call. Throws StatFail if the call fails.
status(s:Str)
Send status to standard error.
Later status output will be treated specially by the shell. It will have suitable representation in the UI.
Use status() when it's semantically a status - a task that's being done.
Str(s:Str, target_width:Int)
Pad a string to given width with spaces
Type(name:Str, doc:Any)
Create a new type. Do not use directly. Use "type MyType".
warn(s:Str)
Write error message to standard error.
write(s:Str, fd:Int)
Write data to a file referenced by file descriptor.
WARNING: Incomplete implementation.
TODO: handle errors, throw exceptions.
TODO: handle possible EINTR.
write(s:Str, p:Pipe)
Write to Pipe. TODO: document if it throws.
~(a:Str, b:Str)
EXPERIMENTAL. For use with AWS Anchors.
~(s:Str, pfx:Pfx)
Check whether s starts with pfx.
~(s:Str, sfx:Sfx)
Check whether s ends with pfx.
~(s:Str, r:RegExp, offset:Int=0, options:Int=0)
Find PCRE regular expression in s. Empty string without options returns MatchN. Throws Error if more than 20 captures are used or if there is an error during matching.
~~(s:Str, r:RegExp, collect_unmatched:Bool=false)
Find all non-overlapping matches of regular expression in a string.