Skip to content

Data Manipulation

Classes

json_array

Methods

add

Add a value to the end of a JSON array.

void json_array::add(var@ value);

Arguments:
Example:
void main() {
	json_array@ arr = parse_json("[]");
	for (uint i = 0; i < 5; i++)
		arr.add(random(1, 100));
	alert("Filled array", arr.stringify());
}
clear

Clears the JSON array completely.

void json_array::clear();

Example:
void main() {
	string data = "[1, 2, 3, 4, 5]";
	json_array@ arr = parse_json(data);
	alert("Before being cleared", arr.stringify());
	arr.clear();
	alert("After being cleared", arr.stringify());
}
is_array

Determine if the value at the given index is a JSON array or not.

bool json_array::is_array(uint index);

Arguments:
Returns:

bool: true if the value at the specified index is a JSON array, false otherwise.

Example:
void main() {
	json_array@ arr = parse_json("[[1, 2], [3, 4]]");
	alert("Info", arr.is_array(1) ? "It is an array": "It is not an array");
}
is_null

Determine if the value at the given index is null.

bool json_array::is_null(uint index);

Arguments:
Returns:

bool: true if the value at the specified index is null, false otherwise.

Example:
void main() {
	json_array@ arr = parse_json("[1, 2, null, 4]");
	alert("Info", "Index 1 " + (arr.is_null(1) ? " is null": " is not null"));
	alert("Info", "Index 2 " + (arr.is_null(2) ? " is null": " is not null"));
}
is_object

Determine if the value at the given index is a JSON object.

bool json_array::is_object(uint index);

Arguments:
Returns:

bool: true if the value at the specified index is a JSON object, false otherwise.

Example:
void main() {
	json_array@ arr = parse_json("""[{}, {}, "test", {}]""");
	alert("Info", "Position 0 " + (arr.is_object(0) ? "is an object" : "is not an object"));
	alert("Info", "Position 2 " + (arr.is_object(2) ? "is an object": "is not an object"));
}
remove

Remove a value from the JSON array given an index.

void json_array::remove(uint index);

Arguments:
Example:
void main() {
	string data = """[47, 4, 584, 43, 8483]""";
	json_array@ arr = parse_json(data);
	alert("Initial", arr.stringify());
	arr.remove(2);
	alert("After removal", arr.stringify());
}
size

Return the size of the JSON array.

uint json_array::size();

Example:
void main() {
	string data = "[";
	for (int i = 0; i < random(10, 20); i++)
		data += i + ", ";
	// Strip off the trailing comma and space character, as they'll make the JSON throw a syntax error.
	data.erase(data.length() - 1);
	data.erase(data.length() - 1);
	data += "]";
	clipboard_set_text(data);
	json_array@ arr = parse_json(data);
	alert("Info", "The JSON array contains " + arr.size() + " items");
}
stringify

Return the JSON array as a string.

string json_array::stringify(int indent = 0, int step = -1);

Arguments:
Returns:

string: the JSON array as a string.

Example:
void main() {
	string data = """["Apple", "Banana", "Orange", "Lemon"]""";
	json_array@ arr = parse_json(data);
	alert("JSON array", arr.stringify(4));
}

Operators

get_opIndex

Get a value out of the JSON array with literal syntax, for example arr[2].

var@ json_array::get_opIndex(uint index) property;

Arguments:
Returns:

var@: a handle to the object, or null if not found.

Example:
void main() {
	json_array@ cats = parse_json("""["Athena", "Willow", "Punk", "Franky", "Yoda", "Waffles"]""");
	alert("The third cat is", cats[2]);
	for (uint i = 0; i < cats.size(); i++)
		alert("Awww", "Hi " + cats[i]);
}
opCall

Get a value out of a JSON array using a query.

var@ json_array::opCall(string query);

Arguments:
Returns:

var@: a handle to the object, or null if it couldn't be found.

Remarks:

Queries are formatted like so, using key names and indexes separated with ".":

world.player.0

It can be as nested as you like.

Example:
void main() {
	string data = """[[["Colorado", "Kansas", "Minnesota"]]]""";
	json_array@ arr = parse_json(data);
	string location = arr("0.0.0");
	alert("My favorite state is", location);
}
set_opIndex

Set a value in the JSON array at a particular position.

void json_array::set_opIndex(uint index, var@ value) property;

Arguments:
Example:
void main() {
	json_array@ arr = parse_json("[]");
	arr[0] = "meow";
	alert("Info", "Cats say " + arr[0]);
}

Properties

empty

Determine if the JSON array is empty.

const bool json_array::empty;

Example:
void main() {
	json_array@ arr = parse_json("[]");
	json_array@ arr2 = parse_json("[1, 2, 3]");
	alert("First is empty", arr.empty ? "true" : "false");
	alert("Second is empty", arr2.empty ? "true" : "false");
}
escape_unicode

Determines the amount of escaping that occurs in JSON parsing.

bool json_array::escape_unicode;

Remarks:

If this property is true, escaping will behave like normal. If it's false, only the characters absolutely vital to JSON parsing are escaped.

json_object

Methods

clear

Clears the JSON object completely.

void json_object::clear();

Example:
void main() {
	string data = """{"numbers": [1, 2, 3, 4, 5]}""";
	json_object@ o = parse_json(data);
	alert("Before being cleared", o.stringify());
	o.clear();
	alert("After being cleared", o.stringify());
}
exists

Determine if a key exists in the JSON object.

bool json_object::exists(const string&in key) const;

Arguments:
Returns:

bool: true if the key exists, false if not.

Example:
void main() {
	string data = """{"engine": "NVGT"}""";
	json_object@ o = parse_json(data);
	alert("Info", (o.exists("engine") ? "The key exists" : "The key does not exist"));
}
get_keys

Get a list of all the keys in the JSON object.

string[]@ json_object::get_keys();

Returns:

string[]@: a handle to an array of strings containing all the keys of the JSON object.

Remarks:

Note that this function isn't recursive, you only get the keys for the object you're running this function on, not any child objects.

Example:
void main() {
	string data = """{"thing": 1, "other_thing": "test", "another": true}""";
	json_object@ o = parse_json(data);
	string[]@ keys = o.get_keys();
	alert("The keys are", join(keys, ", "));
}
is_array

Determine if the value with the given key is a JSON array or not.

bool json_object::is_array(string key);

Arguments:
Returns:

bool: true if the value with the specified key is a JSON array, false otherwise.

Example:
void main() {
	string data = """{"classes": ["json_object", "json_array"]}""";
	json_object@ o = parse_json(data);
	alert("Info", o.is_array("classes") ? "array": "nonarray");
}
is_null

Determine if the value with the given key is null.

bool json_object::is_null(string key);

Arguments:
Returns:

bool: true if the value with the specified key is null.

Example:
void main() {
	string data = """{"brain": null}""";
	json_object@ o = parse_json(data);
	alert("Info", o.is_null("brain") ? "null" : "not null");
}
is_object

Determine if the value with the given key is a JSON object or not.

bool json_object::is_object(string key);

Arguments:
Returns:

bool: true if the value with the specified key is a JSON object, false otherwise.

Example:
void main() {
	string data = """{"json_object": {\}\}""";
	json_object@ o = parse_json(data);
	alert("Info", o.is_object("json_object") ? "Object" : "Non-object");
}
remove

Remove a value from the JSON object given a key.

void json_object::remove(const string&in key);

Arguments:
Example:
void main() {
	string data = """{"name": "Quin"}""";
	json_object@ o = parse_json(data);
	alert("Initial", o.stringify());
	o.remove("name");
	alert("After removal", o.stringify());
}
set

Set a value in a JSON object.

void json_object::set(const string&in key, var@ value);

Arguments:
Example:
void main() {
	json_object@ o = parse_json("{}");;
	o.set("nvgt_user", true);
	alert("Info", (bool(o["nvgt_user"]) ? "You are an NVGT user" : "You are not a regular NVGT user"));
}
size

Return the size of the JSON object.

uint json_object::size();

Remarks:

Note that this function returns the number of top-level keys; it's not recursive.

Example:
void main() {
	string data = """{"numbers": [1, 2, 3, 4, 5]}""";
	json_object@ o = parse_json(data);
	alert("Info", "The json object's size is " + o.size());
}
stringify

Return the JSON object as a string.

string json_object::stringify(int indent = 0, int step = -1);

Arguments:
Returns:

string: the JSON object as a string.

Example:
void main() {
	string data = """{"name": "Quin", "age": 18}""";
	json_object@ o = parse_json(data);
	alert("JSON object", o.stringify(4));
}

Operators

get_opIndex

Get a value out of the JSON object with literal syntax, for example json["key"].

var@ json_object::get_opIndex(const string&in key) property;

Arguments:
Returns:

var@: a handle to the object, or null if not found.

Example:
void main() {
	string data = """{"name": "Quin", "age": 18}""";
	json_object@ o = parse_json(data);
	alert("Info", o["name"] + " is " + o["age"]);
}
opCall

Get a value out of a JSON object using a query.

var@ json_object::opCall(const string&in query);

Arguments:
Returns:

var@: a handle to the object, null if not found.

Remarks:

Queries are formatted like so:

first_field.subfield.subfield

It can be as nested as you like.

This method also works on json_array's, and you access array indexes just with their raw numbers, like so:

world.players.0

Example:
void main() {
	string data = """{"countries":{"us":["Colorado", "Kansas", "Minnesota"]\}\}""";
	json_object@ o = parse_json(data);
	string locations = o("countries.us");
	alert("My favorite states are", locations);
}
set_opIndex

Set a value in the JSON object with a particular key.

void json_object::set_opIndex(const string&in key, var@ value) property;

Arguments:
Example:
void main() {
	json_object@ o = parse_json("{}");
	o["name"] = "Quin";
	alert("Hi", "I am " + o["name"]);
}

Properties

escape_unicode

Determines the amount of escaping that occurs in JSON parsing.

bool json_object::escape_unicode;

Remarks:

If this property is true, escaping will behave like normal. If it's false, only the characters absolutely vital to JSON parsing are escaped.

pack_file

A pack file stores many files inside one larger file, usually so that a game's sounds and other assets can be distributed together and optionally encrypted.

pack_file();

Remarks:

A pack_file object is inactive when created. Call create() to start writing a new pack, or open() to read an existing one. A pack is either being written or being read, never both: you cannot add files to a pack that was opened for reading, and you cannot read files out of a pack until it has been written, closed, and opened again.

Packs can be encrypted by passing a key to create(). The same key must then be passed to open(), otherwise opening fails.

Packs can also be embedded into your compiled game with the #pragma embed packname.dat directive. To open an embedded pack, put an asterisk before its name, for example open("*packname.dat"). For BGT compatibility, opening a pack called * opens the first embedded pack. This also works when you run the script from source, in which case the pack is read from disk instead.

Functions that load assets, such as sound::load(), accept a pack_interface handle. A pack_file converts to a pack_interface implicitly, so you can pass one directly or assign it to sound_default_pack. To get the pack_file back from a pack_interface handle, use cast<pack_file@>().

Files inside a pack are identified by name. Names are case sensitive, must be valid UTF-8, and must be unique within the pack.

This class replaces the older pack class, which is now only available through the legacy_sound plugin. The two classes use different file formats.

Example:

void main() {
	pack_file p;
	if (!p.create("example.dat")) {
		alert("Error", "Unable to create the pack.");
		return;
	}
	p.add_memory("greeting.txt", "Hello from inside a pack!");
	p.close();
	if (!p.open("example.dat")) {
		alert("Error", "Unable to open the pack.");
		return;
	}
	datastream@ ds = p.get_file("greeting.txt");
	alert("greeting.txt", ds.read());
	ds.close(); // The pack file can't be deleted while a stream still has it open.
	p.close();
	file_delete("example.dat");
}

Methods

add_file

Copy a file from disk into a pack that is open for writing.

bool pack_file::add_file(const string&in filename, const string&in internal_name);

Arguments:
Returns:

bool: true if the file was added, false otherwise.

Remarks:

This fails if the pack was not opened with create(), if the file on disk can't be read, or if the pack already contains a file with the given internal name. Files cannot be replaced or removed once added.

The internal name can be anything, including a path such as sounds/step.ogg. Slashes have no special meaning inside a pack, they are simply part of the name.

Example:
void main() {
	file f;
	f.open("hello.txt", "wb");
	f.write("Hello from disk!");
	f.close();
	pack_file p;
	p.create("example.dat");
	if (!p.add_file("hello.txt", "texts/hello.txt")) alert("Error", "Unable to add the file.");
	p.close();
	p.open("example.dat");
	alert("texts/hello.txt", p.get_file("texts/hello.txt").read());
	p.close();
	file_delete("hello.txt");
	file_delete("example.dat");
}
add_memory

Add a file to a pack that is open for writing, using data from a string.

bool pack_file::add_memory(const string&in internal_name, const string&in data);

Arguments:
Returns:

bool: true if the data was added, false otherwise.

Remarks:

This fails if the pack was not opened with create(), or if the pack already contains a file with the given internal name. Files cannot be replaced or removed once added.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("level1.txt", "a maze of twisty little passages");
	alert("Adding a second file with the same name", p.add_memory("level1.txt", "something else") ? "succeeded" : "failed");
	p.close();
	file_delete("example.dat");
}
add_stream

Add a file to a pack that is open for writing, using data read from a datastream.

bool pack_file::add_stream(const string&in internal_name, datastream@ ds);

Arguments:
Returns:

bool: true if the data was added, false otherwise.

Remarks:

The stream is read from its current position until its end, so anything before the current position is not included. This lets you add any kind of datastream to a pack, such as a file, a decoder, or a stream from another pack, without loading all of it into memory first.

This fails if the pack was not opened with create(), if the stream is null or can't be read from, or if the pack already contains a file with the given internal name.

Example:
void main() {
	datastream ds("HEADERthe actual data");
	ds.seek(6); // Skip the header.
	pack_file p;
	p.create("example.dat");
	p.add_stream("data.bin", ds);
	p.close();
	p.open("example.dat");
	alert("data.bin", p.get_file("data.bin").read());
	p.close();
	file_delete("example.dat");
}
close

Close the pack, finishing it first if it was being written.

bool pack_file::close();

Returns:

bool: true if a pack was open and is now closed, false if no pack was open.

Remarks:

When a pack that was opened with create() is closed, its table of contents is written to the file. A pack that was never closed cannot be opened again. Packs are also closed automatically when the pack_file object is destroyed, or when create() or open() is called on the same object.

Any datastreams previously returned by get_file() stay usable after the pack is closed.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("data.txt", "some data");
	alert("Closing an open pack", p.close() ? "succeeded" : "failed");
	alert("Closing it again", p.close() ? "succeeded" : "failed");
	file_delete("example.dat");
}
create

Create a new pack file on disk and open it for writing.

bool pack_file::create(const string&in filename, const string&in key = "");

Arguments:
Returns:

bool: true if the pack was created, false otherwise.

Remarks:

If a file already exists at the given path, it is replaced. If this object already had a pack open, that pack is closed first.

While the pack is open for writing, add files to it with add_file(), add_memory() or add_stream(). The pack is not complete until close() is called or the object is destroyed, because that is when its table of contents is written.

If you pass a key here, you must pass the same key to open() to read the pack later.

Example:
void main() {
	pack_file p;
	if (!p.create("secret.dat", "my password")) {
		alert("Error", "Unable to create the pack.");
		return;
	}
	p.add_memory("note.txt", "Nobody can read this without the key.");
	p.close();
	alert("Encrypted pack opens without the key", p.open("secret.dat") ? "yes" : "no");
	alert("Encrypted pack opens with the key", p.open("secret.dat", "my password") ? "yes" : "no");
	p.close();
	file_delete("secret.dat");
}
extract_file

Copy a file out of the pack and save it to disk.

bool pack_file::extract_file(const string&in internal_name, const string&in file_on_disk);

Arguments:
Returns:

bool: true if the file was extracted, false otherwise.

Remarks:

This fails if the pack is not open for reading, if it doesn't contain the requested file, or if the destination can't be written.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("readme.txt", "Thanks for playing!");
	p.close();
	p.open("example.dat");
	if (p.extract_file("readme.txt", "readme_extracted.txt")) {
		file f("readme_extracted.txt", "rb");
		alert("Extracted file", f.read());
		f.close();
		file_delete("readme_extracted.txt");
	}
	p.close();
	file_delete("example.dat");
}
file_exists

Determine whether a file with a given name exists in the pack.

bool pack_file::file_exists(const string&in filename);

Arguments:
Returns:

bool: true if the pack contains the file, false if not or if no pack is open.

Remarks:

This works for packs opened with either create() or open(). Names are case sensitive, so music.ogg and Music.ogg are different files.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("music.ogg", "not really music");
	alert("music.ogg exists", p.file_exists("music.ogg") ? "yes" : "no");
	alert("Music.ogg exists", p.file_exists("Music.ogg") ? "yes" : "no");
	p.close();
	file_delete("example.dat");
}
get_file

Get a datastream for reading a file from the pack.

datastream@ pack_file::get_file(const string&in filename, const string&in encoding = "", int byteorder = STREAM_BYTE_ORDER_NATIVE);

Arguments:
Returns:

datastream@: a stream for reading the file. If the file doesn't exist or the pack is not open for reading, an inactive stream is returned instead, so check its active property.

Remarks:

Each stream is independent of the pack_file object, so you can read several files at once, and a stream stays readable after the pack is closed. The stream supports seeking, and its positions are relative to the start of the file inside the pack.

Data is read from disk as you read from the stream rather than all at once, so this is suitable for large files.

Each stream keeps the pack file open on disk until the stream is closed or destroyed. On some platforms, including Windows, this means the pack can't be deleted or replaced while any of its streams are still in use.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("numbers.txt", "0123456789");
	p.close();
	p.open("example.dat");
	datastream@ ds = p.get_file("numbers.txt");
	ds.seek(4);
	alert("3 characters from position 4", ds.read(3));
	datastream@ missing = p.get_file("missing.txt");
	alert("Stream for a missing file is active", missing.active ? "yes" : "no");
	ds.close(); // The pack file can't be deleted while a stream still has it open.
	p.close();
	file_delete("example.dat");
}
get_file_size

Get the size of a file in the pack.

int64 pack_file::get_file_size(const string&in filename);

Arguments:
Returns:

int64: the size of the file in bytes, or -1 if the file doesn't exist or no pack is open.

Remarks:

This works for packs opened with either create() or open().

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("data.txt", "1234567890");
	alert("Size of data.txt", p.get_file_size("data.txt") + " bytes");
	alert("Size of a missing file", p.get_file_size("missing.txt"));
	p.close();
	file_delete("example.dat");
}
list_files

Get the names of every file in the pack.

string[]@ pack_file::list_files() const;

Returns:

string[]@: a handle to an array containing the internal name of every file in the pack. The array is empty if no pack is open.

Remarks:

This works for packs opened with either create() or open(). The order of the names is not guaranteed, so sort the array if you need a particular order.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("c.txt", "");
	p.add_memory("a.txt", "");
	p.add_memory("b.txt", "");
	string[]@ names = p.list_files();
	names.sort_ascending();
	alert("Files in the pack", join(names, ", "));
	p.close();
	file_delete("example.dat");
}
open

Open an existing pack file for reading.

bool pack_file::open(const string&in filename, const string&in key = "", uint64 pack_offset = 0, uint64 pack_size = 0);

Arguments:
Returns:

bool: true if the pack was opened, false otherwise.

Remarks:

Opening fails if the file doesn't exist, isn't a pack created by pack_file, is damaged, or if the key is wrong or missing. If this object already had a pack open, that pack is closed first, even if opening the new one fails.

To open a pack embedded with #pragma embed, prefix its name with an asterisk, for example open("*sounds.dat"). A filename of just * opens the first embedded pack, for BGT compatibility.

The pack_offset and pack_size arguments let you read a pack that is stored inside some other file. The data in that range must be an entire pack exactly as create() wrote it.

Example:
void main() {
	// Create a small pack so that there's something to open.
	pack_file p;
	p.create("example.dat");
	p.add_memory("a.txt", "first file");
	p.add_memory("b.txt", "second file");
	p.close();
	if (!p.open("example.dat")) {
		alert("Error", "Unable to open the pack.");
		return;
	}
	alert("Files in the pack", join(p.list_files(), ", "));
	p.close();
	file_delete("example.dat");
}

Properties

active

Determine whether the pack is currently open, either for reading or for writing.

const bool pack_file::active;

Example:
void main() {
	pack_file p;
	alert("Active before create", p.active ? "yes" : "no");
	p.create("example.dat");
	alert("Active after create", p.active ? "yes" : "no");
	p.close();
	file_delete("example.dat");
}
file_count

The number of files in the pack.

const int64 pack_file::file_count;

Remarks:

This is -1 if no pack is open. While a pack is being written, it counts the files added so far.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	p.add_memory("a.txt", "a");
	p.add_memory("b.txt", "b");
	alert("Files in the pack", p.file_count);
	p.close();
	file_delete("example.dat");
}
pack_name

The filename of the pack that is currently open.

const string pack_file::pack_name;

Remarks:

This is the path the pack was opened or created with, exactly as it was passed. For an embedded pack, it is the path of the file the pack was actually read from. It is empty if no pack has been opened yet.

Example:
void main() {
	pack_file p;
	p.create("example.dat");
	alert("Pack name", p.pack_name);
	p.close();
	file_delete("example.dat");
}

regexp

The regexp object allows for the easy usage of regular expressions in NVGT.

regexp(const string&in pattern, int options = RE_UTF8);

Arguments:

Remarks:

Regular expressions are a language used for matching, substituting, and otherwise manipulating text. To learn about the regular expression syntax that nVGT uses (called PCRE), see this link: https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions

Example:

void main() {
	regexp re("^[a-z]{2,4}$"); // Will match any lowercase alphabetical string between 2 and 4 characters in length.
	string compare = input_box("Text", "Enter the text to check against the regular expression.");
	if (compare.empty()) {
		alert("Regular expression tester", "You didn't type a string to test.");
		exit(1);
	}
	bool matches = re.match(compare);
	alert("The regular expression", matches ? "matches" : "does not match");
}

methods

match

Determine if the regular expression matches against a particular string or not.

  1. bool regexp::match(const string&in subject, uint64 offset = 0);
  2. bool regexp::match(const string&in subject, uint64 offset, int options);
Arguments (1):
Arguments (2):
Returns:

bool: true if the regular expression matches the given string, false if not.

Enums

regexp_options

This enum holds various constants that can be passed to the regular expression classes in order to change their behavior.

Notes:

Portions of this regexp_options documentation were Copied from POCO header files.

See the PCRE documentation for more information.

Constants:

Functions

ascii_to_character

Return the character that corresponds to the given ascii value.

string ascii_to_character(uint8 ascii);

Arguments:

Returns:

string: the character for the given ascii value.

Example:

void main() {
	uint8 ascii = parse_int(input_box("ASCII value", "Enter the ascii value to convert."));
	alert("Info", ascii + " has a value of " + ascii_to_character(ascii));
}

character_to_ascii

Return the ascii value for the given character.

uint8 character_to_ascii(const string&in character);

Arguments:

Returns:

uint8: the ascii value for the given character.

Remarks:

If a string containing more than one character is passed as input to this function, only the left-most character is checked.

If an empty string is passed, the function returns 0.

Example:

void main() {
	string character = input_box("Character", "Enter a character to convert");
	if (character.is_empty()) {
		alert("Error", "You did not type anything.");
		exit();
	}
	if (character.length() != 1) { // The user typed more than one character.
		alert("Error", "You must only type a single character.");
		exit();
	}
	alert("Info", character + " has an ascii value of " + character_to_ascii(character));
}

join

turn an array into a string, with each element being separated by the given delimiter.

string join(const string[]@ elements, const string&in delimiter);

Arguments:

Returns:

string: the given array as a string.

Remarks:

If an empty array is passed, a blank string is returned.

The delimiter is not appended to the end of the last item in the returned string. For example if an array contains [1, 2, 3] and you join it with a delimiter of . (period), the result would be "1.2.3" without an extra delimiter appended.

Example:

void main() {
	string[] names = {"Sam", "Quin", "Patrick"};
	string people = join(names, ", ");
	alert("Info", people);
}

number_to_words

Convert a number into its string equivalent, for example, one thousand four hundred and fifty six.

string number_to_words(int64 the_number, bool include_and = true);

Arguments:

Returns:

string: the specified number, converted to a readable string.

Remarks:

At this time, this function only produces English results.

Example:

void main() {
	int64 num = random(1000, 100000);
	string result = number_to_words(num);
	alert("Info", num + " as a string is " + result);
}

regexp_match

Test if the text matches the specified regular expression.

bool regexp_match(const string&in text, const string&in pattern);

Arguments:

Returns:

bool: true if the text matches the given regular expression, false otherwise.

Remarks:

If you would like a lower level interface to regular_expressions in NVGT, check out the regexp class which this function more or less wraps.

To learn more about regular expressions, visit: https://en.wikipedia.org/wiki/Regular_expression

Example:

void main() {
	bool match = regexp_match("Test", "^[a-zA-Z]+$");
	alert("the regular expression", match ? "matches" : "does not match");
}

string_base32_decode

Decodes a string from base32.

string string_base32_decode(const string&in the_data);

Arguments:

Returns:

String: The decoded string on success or an empty string on failure.

Remarks:

You can learn more about the base32 format here.

Example:

void main() {
	string text = input_box("Text", "Enter the text to decode.");
	if (text.is_empty()) {
		alert("Error", "You did not type any text.");
		exit();
	}
	alert("Info", string_base32_decode(text));
}

string_base32_encode

Encodes a string as base32.

string string_base32_encode(const string&in the_data);

Arguments:

Returns:

String: The encoded string on success or an empty string on failure.

Remarks:

You can learn more about the base32 format here.

Example:

void main() {
	string text = input_box("Text", "Enter the text to encode.");
	if (text.is_empty()) {
		alert("Error", "You did not type any text.");
		exit();
	}
	alert("Info", string_base32_encode(text));
}

string_base32_normalize

Normalize a string to conform to the base32 spec.

string string_base32_normalize(const string&in the_data);

Arguments:

Returns:

string: the normalized string.

Remarks:

The primary thing this function does is to skip separators, convert all letters to uppercase, and make sure the length is a multiple of 8.

To learn more about base32, click here.

Example:

void main() {
	string text = input_box("Text", "Enter the text to normalize.");
	if (text.is_empty()) {
		alert("Error", "You didn't type anything.");
		exit();
	}
	alert("Info", string_base32_normalize(text));
}

string_base64_decode

Decodes a string which has previously been encoded in base64.

string string_base64_decode(const string&in the_data, string_base64_options options = STRING_BASE64_PADLESS);

Arguments:

Returns:

String: The decoded string on success or an empty string on failure.

Remarks:

The following options may be passed to the options argument of this function:

You can learn more about base64 here.

Example:

void main() {
	alert("example", string_base64_decode("aGVsbG8=")); // Should print "hello".
}

string_base64_encode

Encodes a string as base64.

string string_base64_encode(const string&in the_data, string_base64_options options = STRING_BASE64_DEFAULT);

Arguments:

Returns:

String: The encoded string on success or an empty string on failure.

Remarks:

The following options may be passed to the options argument of this function:

You can learn more about base64 here.

Example:

void main() {
	alert("example", string_base64_encode("Hello")); // Should print SGVsbG8=
}