ManagedString¶
Overview¶
ManagedString
is a safe representation of a text string in the micro:bit runtime.
A string is simply a sequence of characters such as "joe" or "micro:bit". In the C language, the end of the string is marked by a special character (a NULL character, commonly with the value zero). Simple strings are often represented as literal character arrays:
uBit.display.scroll("HELLO");
which is almost exactly the same as:
char message[6] = {'H', 'E', 'L', 'L', 'O', 0};
uBit.display.scroll(message);
Although fantastically simple, strings of this form are well known to lead to memory leaks and be sources of bugs in code (especially when the programmers are still learning!).
As a result, most modern high level languages such as Java, C#, Javascript and TouchDevelop do not use strings of this format. Instead, they provide code that is capable of ensuring strings remain safe.
ManagedString
provides this equivalent functionality for the micro:bit, as a building block for higher level languages. However, it can also makes programming the micro:bit in C easier too!
Note
This is a managed type. This means that it will automatically use and release memory as needed. There is no need for you to explicitly free or release memory when you're done - the memory will be freed as soon as the last piece of code stops using the data.
Using strings¶
Creating strings¶
Instances of ManagedString
are simple to create - just create them like a variable, and provide the text or number you would like to build the string from.
For example:
1 2 3 |
|
The runtime will also create a ManagedString
for you from a number or quoted literal anytime a function requires a ManagedString
.
In the example below, even though the scroll function of uBit.display
expects a ManagedString
,
it is totally fine to pass a literal value in quotes or a number (or in fact, any parameter that is listed in the API section as a legal constructor will work):
1 2 3 4 5 6 7 8 |
|
Manipulating Strings¶
ManagedStrings are immutable, meaning that once created, they cannot be changed. However, you can join them, search them, extract characters from them and create other strings!
The micro:bit runtime makes use of operator overloading to keep this easy to use. In other words, we make use of the =
+
<
>
and ==
operators to let you easily assign and compare strings. Although this may sound complex, it is easy once you see how to do it.
Here is how you could join together more than one string, and assign it to a new one:
Example
1 2 3 4 5 6 7 8 |
|
Here is how you could compare strings alphabetically in a similar manner:
Example
1 2 3 4 5 6 7 8 9 10 11 |
|
You can also determine the length of a string, extract parts of strings, retrieve individual characters at a given index or convert a ManagedString to a C-style character array using the length
, substring
, charAt
and toCharArray
functions respectively.
See the API documentation below for further details.
API¶
operator=¶
ManagedString operator=( const ManagedString & s)
Description
Copy assign operation.
Called when one ManagedString is assigned the value of another.
If the ManagedString being assigned is already referring to a character buffer, decrement the reference count and free up the buffer as necessary.
Then, update our character buffer to refer to that of the supplied ManagedString , and increase its reference count.
Parameters
const ManagedString &s - The ManagedString to copy.
Example
ManagedString s("abcd");
ManagedString p("efgh");
p = s // p now points to s, s' ref is incremented
operator==¶
bool operator==( const ManagedString & s)
Description
Equality operation.
Called when one ManagedString is tested to be equal to another using the '==' operator.
Parameters
const ManagedString &s - The ManagedString to test ourselves against.
Returns
true if this ManagedString is identical to the one supplied, false otherwise.
Example
DeviceDisplay display;
ManagedString s("abcd");
ManagedString p("efgh");
if(p == s)
display.scroll("We are the same!");
else
display.scroll("We are different!"); //p is not equal to s - this will be called
operator!=¶
bool operator!=( const ManagedString & s)
Description
Inequality operation.
Called when one ManagedString is tested to be not equal using the '!=' operator.
Parameters
const ManagedString &s - The ManagedString to test ourselves against.
Returns
true if this ManagedString is identical to the one supplied, false otherwise.
Example
DeviceDisplay display;
ManagedString s("abcd");
ManagedString p("efgh");
if(p != s)
display.scroll("We are different!");
else
display.scroll("We are the same!");
operator<¶
bool operator<( const ManagedString & s)
Description
Inequality operation.
Called when one ManagedString is tested to be less than another using the '<' operator.
Parameters
const ManagedString &s - The ManagedString to test ourselves against.
Returns
true if this ManagedString is alphabetically less than to the one supplied, false otherwise.
Example
DeviceDisplay display;
ManagedString s("a");
ManagedString p("b");
if(s < p)
display.scroll("a is before b!"); //a is before b
else
display.scroll("b is before a!");
operator>¶
bool operator>( const ManagedString & s)
Description
Inequality operation.
Called when one ManagedString is tested to be greater than another using the '>' operator.
Parameters
const ManagedString &s - The ManagedString to test ourselves against.
Returns
true if this ManagedString is alphabetically greater than to the one supplied, false otherwise.
Example
DeviceDisplay display;
ManagedString s("a");
ManagedString p("b");
if(p>a)
display.scroll("b is after a!"); //b is after a
else
display.scroll("a is after b!");
substring¶
ManagedString substring( int16_t start, int16_t length)
Description
Extracts a ManagedString from this string, at the position provided.
Parameters
int16_tstart - The index of the first character to extract, indexed from zero.int16_tlength - The number of characters to extract from the start position
Returns
a ManagedString representing the requested substring.
Example
DeviceDisplay display;
ManagedString s("abcdefg");
display.scroll(s.substring(0,2)) // displays "ab"
charAt¶
char charAt( int16_t index)
Description
Provides a character value at a given position in the string, indexed from zero.
Parameters
int16_tindex - The position of the character to return.
Returns
the character at posisiton index, zero if index is invalid.
Example
DeviceDisplay display;
ManagedString s("abcd");
display.scroll(s.charAt(1)) // scrolls "b"
toCharArray¶
const char * toCharArray()
Description
Provides an immutable 8 bit wide character buffer representing this string.
Returns
a pointer to the character buffer.
length¶
int16_t length()
Description
Determines the length of this ManagedString in characters.
Returns
the length of the string in characters.
Example
DeviceDisplay display;
ManagedString s("abcd");
display.scroll(s.length()) // scrolls "4"
Component Constructor¶
Advanced users only
Do not use this unless you really know what you're doing. It's usually best to use uBit
.
friend ManagedString( codal::operator+ )
Description
Concatenates two strings.
Parameters
codal::operator+
Returns
a new ManagedString representing the joined strings.
Example
DeviceDisplay display;
ManagedString s("abcd");
ManagedString p("efgh")
display.scroll(s + p) // scrolls "abcdefgh"