The modern and fun way to program.
std.langThe box std.lang is imported by default, because it contains all the methods
of strings, lists and tuples.
StringClass String provides all the methods invokable from a string object.
This class is very special, because it’s the only one of the SSL which
is not completely a Smudge class, because it is an independent type.
A string can be instanced by enclosing its content with a couple of quotes
' or double quotes ".
/*
* These are all strings:
*/
"string"
"Hello, world!"
'hel" lo" world'
"writin'"
String and UTF-8Smudge’s strings fully support UTF-8 with some
specialized functions (when we treat them, I’ll tell you,
but typically they start with u_). However, to use them
effectively, you should understand how does UTF-8
work: below is a simple explanation.
As you know, ASCII uses only 7 bits to represent all its characters. All its implementations, although, use a whole byte for each character. UTF-8 was born to extend the character set of ASCII, being, at the same way, fully compatible with it.
In fact, all ASCII strings are automatically UTF-8 strings because all of the ASCII characters exist in UTF-8. But how are 128,172 characters represented in a single byte? The answer is: they are not stored in a single byte! Each UTF-8 character is contained in 1, 2, 3 or even 4 bytes following the rule:
| N. of bytes per character | Value in binary |
|---|---|
| 1 | 0xxxxxxx |
| 2 | 110xxxxx 10xxxxxx |
| 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
Where xs are replaced with the Unicode’s codepoint of the characters.
This is why Smudge has two different methods to get the length of a string:
len() which will return the size of the string expressed in bytes
(faster, O(1)).count() which will return the length of the string expressed
in characters (slower, O(N)). This is an example of one of those
charming functions I’ve previously talk about.You should use len() only when working with ASCII strings or you need to
obtain its real memory size (in bytes).
I’ve chosen UTF-8 because of its total compatibility with ASCII and because it’s the cheapest in terms of memory: as I said, strings that use only or mostly ASCII will be up to twice longer in UTF-16 (and up to four times longer in UTF-32). Also:
Some characters can’t be just typed between the quotes (such as
new line, carriage return, tab, any control character, etc.),
so you can enter any character via special sequences called escape
sequences.
| Escape sequences | Value | Description |
|---|---|---|
\' |
0x27 | Single quote |
\" |
0x22 | Double quote |
\\ |
0x5C | Backslash |
\a |
0x07 | Bell |
\b |
0x88 | Backspace |
\f |
0x0C | Form feed - New page |
\n |
0x0A | Form feed - New line |
\r |
0x0D | Carriage return |
\t |
0x09 | Horizontal tab |
\v |
0x0B | Vertical tab |
\NNN |
value NNN | Arbitrary octal value (only ASCII) |
\xNN |
value NN | Arbitrary hexadecimal value (only ASCII) |
\uNNNN |
codepoint U+NNNN | Arbitrary hexadecimal 32-bit Unicode codepoint (UTF-8) |
\UNNNNNNNN |
codepoint U+NNNNNNNN | Arbitrary hexadecimal 64-bit Unicode codepoint (UTF-8) |
new () and delete ()Do nothing.
Return null.
idx (i)Calculates the index in bytes from the given index in characters i.
If i is negative, the characters will be counted from the end.
Returns the value of the index resulting or null if it cannot be
calculated (e.g. in case of out of index).
len ()Returns the size of the sring expressed in bytes.
count ([start = 0[, end = len()]])Returns the length (expressed in characters) of the range of the
string from index start (included) to index end (excluded).
start and end are expressed in bytes and can be negative: in that case
they will be counted from the end.
empty ()Returns true if the string is empty, false otherwise.
compare (str[, ignore_case = false])Compares this string to the given string str.
Returns an integer which will be:
str is not a string.Also, the comparision between strings can be done ignoring
cases: just pass ignore_case as true!
u_compare (str[, ignore_case = false])Compares this string to the given string str, like
function compare() does, but supporting all the
UTF-8 characters (when involving cases).
== (str)Compares this string to the given string str.
Returns true if they are equal, false otherwise.
!= (str)Compares this string to the given string str.
Returns true if they are different, false otherwise.
< (str), <= (str), > (str), >= (str)Compare this string to the given string str.
Return true if the string is respectively
less than, less or equal to, greater than or greater or equal
to str.
+ (str)Returns a new string created by concatenating the string with the given
string str. If str is not a string, will be converted by calling its method
to_string().
- (n_chs)Returns a new string without the last n bytes specified by
the given integer n_chs, or null if c_chs is not an integer or is greater
than len().
* (times)Returns a new string whose content will be times reps of the string or
null if times is not an integer. If times is negative, the string will
be reversed. Thus, to simply reverse a string you need only to multiply for -1.
get (idx)Returns a string containing the ASCII character
located at the given index idx (expressed in bytes) or
null in case of out of index (or id idx is not an integer).
If idx is negative, it will be counted from the end of the
string.
u_get (n_ch)Returns a string containing the UTF-8 character
at the given index n_ch (expressed in characters) or
null in case of out of index (or id n_ch is not an integer).
If n_ch is negative, it will be counted from the end of the string.
getc (idx)Returns an integer containing the code of the ASCII character
located at the given index idx (expressed in bytes) or
null in case of out of index (or id idx is not an integer).
If idx is negative, it will be counted from the end of the string.
u_getc (n_ch)Returns an integer containing the UNICODE codepoint of the UTF-8
character located at the given index n_ch (expressed in bytes) or
null in case of out of index (or id n_ch is not an integer).
If n_ch is negative, it will be counted from the end of the string.
contains (str)Returns true if str’s content is contained into
the string, false otherwise.
bytes ()Returns a List containing all the numeric values
of each byte of the string.
chars ()Returns a List of strings cotaining each an ASCII/UTF-8 character
of the origin string.
join (list)Concatenates all the string equivalent of list’s elements (got via a call to
elements’ to_string() method) with the string as separator. list can be
either a List or a Tuple.
Returns the string or null if list is neither a List nor a
Tuple.
ends_with (str)Returns true if the string ends with the given string str, false
otherwise (or null if str is not a string).
starts_with (str)Returns true if the string starts with the given string str,
false otherwise (or null if str is not a string).
hash ()Returns the hash value of the string.
find (str)Returns the index location of the given string str in the string,
returning len() if str is not found, or null if str is not a string.
find_last (str)Same as find (str), but only the last occurrence of str is taken into
account.
substr (start[, end = len()])Returns a copy of the string from index start (included) to index end
(excluded), or null if start > end or if they are not integers.
Just as previous methods, indexes start and end can be negative: in that
case they will be counted from the end of the string.
u_substr (u_start[, u_end = count()])Same as substr(), but uses indexes expressed in characters instead of
bytes.
replace ([to_replace = ''[, replacement = '']])Returns a new string where all the occurrences of string to_replace are
replaced with string replacement, or null if replacement is not a string.
If replacement is an empty string, every occurrences will be simply removed
from the string, while if to_replace is not a string or is an empty string,
the returned string won’t be different to the original.
replace_first ([to_replace = ''[, replacement = '']])Same as replace(), but only the first occurrence is replaced.
split (separators[, skipEmpty = true])Splits the string into tokens separated by any of the string
separators’s bytes. If skipEmpty is set to false, are valid tokens
empty ones, too. Returns a List containing the created tokens or
null if separators is not a string.
trim ()Returns a copy of the string without spacing characters (all ASCII characters <= ‘ ‘, such as control characters, spaces, tabs, newlines, etc.) at the begin end at the end of the string.
upper ()Returns a copy of the string with all the ASCII characters in their uppercase version.
lower ()Returns a copy of the string with all the ASCII characters in their lowercase version.
u_upper ()Returns a copy of the string with all the UTF-8 characters in their uppercase version.
u_lower ()Returns a copy of the string with all the UTF-8 characters in their lowercase version.
clone ()Returns an exact copy of the string, stored in a distinct address of the memory.
to_string ()Returns the string.
iterate ()Returns an instance of StringIterator pointing to the start of the string.
ListThis is the class which provides all the Smudge lists’ functionalities.
A List can be instanced simply with a couple of brackets containing
the elements separed by ,. Lists’ elements could be of any type.
/* A list could contain integers, strings and lists as well */
var my_list = [0, 1, "Hello!", [0, "World!"], xyz];
new () and delete ()Create and destroy the list, shouldn’t be called manually. Return null.
[] (idx)Returns a reference to the element located ad the given index idx,
or null if idx is not an integer or is out of index.
if idx is negative, it will be counted from the end.
+ (lst)Concatenates the list with the given list or tuple lst.
Returns the new list, or null if lst is neither a list nor a tuple.
- (n)Returns the list without the last n elements, or null if n is
not an integer or negative.
* (times)Returns a new list whose content will be times reps of the list or
null if times is not an integer. If times is negative, the list will
be reversed. Thus, to simply reverse a list you need only to multiply for -1.
| (lst)Returns a new list containing all the elements from this and lst lists,
repeating the objects in common only once (works well with lists containing
unique elements), or null if lst is neither a list nor a tuple.
& (lst)Returns a list containing only the objects in common between this
list and lst (works well if each list has only unique elements), ornull
if lst` is neither a list nor a tuple.
== (lst)Returns true if lst is a list and its content is equivalent to this
list’s, false otherwise.
!= (lst)Returns false if lst is a list and its content is equivalent to this
list’s, true otherwise.
get (idx)Returns the element located at the given index idx, or null if idx is
not an integer or if out of range. idx can be negative, in that case it will be
counted from the end.
reserve (size)Reserves enough space in memory for the list to contain size elements.
If size is not an integer or is negative, it does nothing.
Returns null.
resize (size)Resizes the list to size size, objects eventually added will be nulls.
If size is not an integer or is negative, it does nothing.
Returns null.
pop ()Removes the last element to the list or does nothing if the list is empty.
Returns null.
push (obj)Adds object obj to the end of the list.
Returns null.
pop_front ()Removes the first element to the list or does nothing if the list is empty.
Returns null.
push_front (obj)Adds object obj at the beginning of the list.
Returns null.
clone ([start = 0[, end = size()]])Returns a list containing a copy of the objects from index start
(included) to index end (excluded) or null if either start or end
is not an integer or an out of range one. As always, indexes can be negative,
in that case they will be counted from the end of the list.
tuple ([start = 0[, end = size()]])Same as clone(), but returns a Tuple instance instead of a list.
append (lst)Added the content of the list lst to the end of this list or does nothing
if lst is not a list.
Returns null.
insert (idx, obj)Inserts the object obj at the given index idx.
idx can be negative, in that case it will be counted from the end.
Does nothing if idx is not an integer or if it’s out of range.
Returns null.
insert_list (idx, lst)Same as insert(), but inserts the whole content of the list or tuple given
lst. Does nothing if lst is neither a tuple nor a list.
erase ([start = 0[, end = start+1]])Removes elements from index start (included) to index end (excluded)
from the list. Since the end’s default value is start’s plus one,
when only start index is specified, only the object at that index
will be removed. start and end can be negative, in that case they will be
counted from the end. Does nothing when either start or end is not an
integer or the range given is broken (e.g. start < end). ‘Returns null.
copy_list (idx, lst)Copies the content of list or tuple lst in the list starting from index idx
following the table:
| Condition | What happens |
|---|---|
idx is not an integer or lst is not a list or a tuple |
Nothing, returns false. |
idx >= size() |
Nothing, returns false. |
idx < size() and idx + lst.size() <= size() |
Copies lst’s content in this list, starting at given index. Returns true |
idx < size() but idx + lst.size() > size() |
Same as above, but resizes this list in order to contain the whole lst' content. Returns true`. |
find (obj)Searches an equivalent object to obj (checked via operator== ()).
Returns the index of the first occurence, or null if not found.
count (obj)Counts how many equivalent objects to obj (checked via operator== ()).
Returns integer which value is the number counted.
slice ([start = 0[, end = size()]])Alias for clone().
reverse ()Reverses the list in place.
Returns null.
sort ([reversed = false])Sorts the list in place in ascending order if reversed is false or
descending order if reversed is true. The elements of the list are
compared via the operator <, which can be overloaded. Returns null.
unique ()Eliminates all but the first element from every consecutive group of
equivalent elements from the list. Returns null.
to_string ()Returns a string representation of the list (typically elements’
to_string() values separated by a comma inside a couple of brackets).
empty ()Returns true if the list is empty (has no elements), false otherwise.
size ()Returns the number of elements contained by the list.
iterate ()Returns an instance of ListIterator pointing to the start of the list.
TupleTuples are immutable lists. You can instance a Tuple by enclosing its
elements with a couple of round brackets (i.e. ()).
/* Tuples are just like lists, but are immutable. */
var my_tuple = (1, 2, "Hello", ("World", [0, 1, "!"], ()), []);
new () and delete ()Create and destroy the tuple, shouldn’t be called manually. Return null.
+ (lst)Returns a new tuple created by concatenating the tuple to list or tuple
lst, or null if lst is neither a list nor a tuple.
- (n)Returns a new tuple without the last n elements of the tuple, ornull
if n` is not an integer or negative.
* (times)Returns a new tuple whose content will be times reps of the tuple or
null if times is not an integer. If times is negative, the tuple will
be reversed. Thus, to simply reverse a tuple you need only to multiply for -1.
| (tup)Returns a new tuple containing all the elements from this and tup tuples,
repeating the objects in common only once (works well with tuples containing
unique elements), or null if tup is neither a list nor a tuple.
& (tup)Returns a tuple containing only the objects in common between this
tuple and tup (works well if each tuple has only unique elements), ornull
if tup` is neither a list nor a tuple.
== (lst)Returns true if lst is a tuple and its content is equivalent to this
tuple’s, false otherwise.
!= (lst)Returns false if lst is a tuple and its content is equivalent to this
tuple’s, true otherwise.
slice ([start = 0[, end = size()]])Returns a tuple containing a copy of the objects from index start
(included) to index end (excluded), or null if either start or end
is not an integer or an out of range one. As always, indexes can be negative,
in that case they will be counted from the end of the list.
get (idx)Returns the element located at the given index idx, or null if idx is
not an integer or if out of range. idx can be negative, in that case it will be
counted from the end.
list ([start = 0[, end = size()]])Same as slice(), but returns a list.
hash ()Returns the hash value of the tuple.
empty ()Returns true if the tuple is empty (has no elements), false otherwise.
clone ([start = 0[, end = size()]])Returns a tuple containing a copy of the objects from index start
(included) to index end (excluded) or null if either start or end
is not an integer or an out of range one. As always, indexes can be negative,
in that case they will be counted from the end of the list.
to_string ()Returns a string representation of the tuple, very similar to tuple’s with round brackets instead of brackets.
iterate ()Returns an instance of ListIterator pointing to the start of the tuple.
StringIteratorAn Iterator class for String.
new (str)Initializes a new StringIterator starting from the begin of String str.
Returns null.
delete ()Destroy the StringIterator instance.
Returns null.
next ()Advance the iterator. Returns a tuple containing the current item and an integer (1 if it’s a valid item, 0 otherwise).
ListIteratorSame as StringIterator, but for Lists or Tuples.
new (lst), delete (), next ()See StringIterator.
| Home |