-
-
Notifications
You must be signed in to change notification settings - Fork 9
String
String
represents the type of a string value.
Type | Size | Possible Values | Memory Management Model | File |
---|---|---|---|---|
String |
32 bytes | All strings | Ownership | 2.7/String.adept |
StringView |
^ | ^ | ^ | ^ |
record String (array *ubyte, length usize, capacity usize, ownership StringOwnership)
alias StringView = String
Name | Type | Description |
---|---|---|
array |
*ubyte |
Pointer to raw array of characters, not null-terminated |
length |
usize |
Length of array in characters |
capacity |
usize |
Capacity of array in characters |
ownership |
StringOwnership |
Whether this string is responsible for freeing array
|
func __defer__(this *String) void
func __pass__(string POD String) String
func __assign__(this *String, other POD String) void
func __access__(this *String, index usize) *ubyte
func __equals__(lhs String, rhs String) bool
func __not_equals__(lhs String, rhs String) bool
func __add__(lhs String, rhs String) String
func __add__(lhs String, rhs ubyte) String
func __add__(lhs POD String, rhs $T~__primitive__) String
func __multiply__(lhs String, times int) String
func __modulus__(lhs POD String, rhs String) String
func __modulus__(lhs POD String, rhs int) String
func __modulus__(lhs POD String, rhs ptr) String
func __modulus__(lhs POD String, rhs bool) String
func __modulus__(lhs POD String, rhs $T~__primitive__) String
func __less_than__(lhs String, rhs String) bool
func __greater_than__(lhs String, rhs String) bool
func __less_than_or_equal__(lhs String, rhs String) bool
func __greater_than_or_equal__(lhs String, rhs String) bool
func __array__(this *String) *ubyte
func __length__(this *String) usize
String literals are declared using double quotes.
food String = "Spaghetti"
*ubyte | String |
---|---|
Null-Terminated | NOT Null-Terminated |
Must be manually free() d or delete d |
Automatically freed |
No Standard Library Dependencies | Requires 2.7/String.adept
|
Can only be string view for end of string | Can always be string view |
Length is calculated | Length is stored |
Strings are automatically freed when they run out of scope.
import basics
func main {
name String = "Isaac".clone()
// name is freed
}
In order to pass strings around to other scopes, you can use the commit()
method. my_string.commit()
gives up any ownership held by my_string
and returns a copy with the original's ownership:
import basics
func main {
outlives String
if true {
shortlived String = "Apple".clone()
// Transfer ownership to 'outlives'
// 'outlives' now is equal to 'shortlived', but has taken ownership away from it
outlives = shortlived.commit()
}
// outlives is freed
}
- Strings are freed when they go out of scope
- Strings are freed when inside a value that goes out of scope
- Strings can transfer scopes using
new_scoped_string = old_scoped_string.commit()
- Strings can force themselves to own a String using
must_own_a_string.make()
- Strings are freed when they are returned and never assigned
- Strings reference the data of other Strings when assigned to them
- Strings can be cloned using
original.clone()
- Strings can point to specific parts of other Strings
- Variables/Parameters marked as
POD
will not have__pass__
and__defer__
calls automatically made - When
add()
ed to things likeList
, a reference will be used by default - To commit ownership of a string to a list, you must use
list.add(string.commit())
One special thing about String
, is that it can be used both as a regular string and as a string view. The methods span
and range
return a StringView
(which is an alias for String
), where as methods sub
and segment
return a newly allocated String
.
import basics
func main {
original String = "Hello World".clone()
view String = original.span(0, 5)
copy String = original.sub(0, 5)
print(view)
print(copy)
original[0] = 'Y'ub
print(view)
print(copy)
}
Hello
Hello
Yello
Hello
-
func String(null_terminated *ubyte) String
Creates a
String
value from a null-terminated string. The returned value will have ownership of a copy of the string. -
func String(array *ubyte, length usize) String
Creates a
String
value from a character array. The returned value will have ownership of a copy of the string. -
func StringView(null_terminated *ubyte) String
Creates a
String
value from a null-terminated string. The returned value will be a view of the memory supplied. -
func StringView(array *ubyte, length usize) String
Creates a
String
value from a character array. The returned value will be a view of the memory supplied. -
func stringConstant(null_terminated *ubyte) String
Creates a
String
value from a null-terminated string. The returned value will reference the memory supplied. -
func stringTake(null_terminated *ubyte) String
Creates a
String
value from a null-terminated string and takes ownership of freeing the supplied string. The returned value will have ownership of the memory supplied. -
func stringTake(null_terminated *ubyte, capacity usize) String
Creates a
String
value from a null-terminated string and takes ownership of freeing the supplied string. The returned value will have ownership of the memory supplied. -
func contains(haystack String, needle String) bool
Returns whether a string is contained within another string.
-
func contains(haystack String, needle ubyte) bool
Returns whether a string contains a certain character.
-
func startsWith(string String, other POD String) bool
Returns whether a string starts with the contents of another string.
-
func endsWith(string String, other POD String) bool
Returns whether a string ends with the contents of another string.
-
func longestLength(string String) usize
Returns the longest line length in a string. Lines are separated by
'\n'
characters. -
func count(string String, character ubyte) usize
Returns the number of occurrences of a certain character within a string.
-
func count(string String, substring String) usize
Returns the number of occurrences of a substring within a string.
-
func uppercase(string String) String
Returns an uppercased copy of a string.
-
func lowercase(string String) String
Returns a lowercased copy of a string.
-
func trim(string String) String
Returns a trimmed copy of a string.
-
func toString(value POD String) String
Identity function that returns the supplied string.
-
func compare(this *String, other String) int
Compares two strings.
- Returns
-1
ifthis
should come first alphabetically. - Returns
1
ifthis
should come second alphabetically. - Returns
0
ifthis
is equal toother
.
- Returns
-
func commit(this *String) String
Changes the ownership of a
String
to beStringOwnership::REFERENCE
. If theString
previously had ownership, then aString
value will be returned withStringOwnership::GIVEN
, otherwise the originalString
will be returned. This method is used for transferring ownership of internal data ofString
values to otherString
values. -
func donate(this *String) String
If the
String
has ownership, then the subject's ownership will be changed toStringOwnership::DONOR
and aString
value will be returned withStringOwnership::GIVEN
, otherwise the originalString
will be returned. This method is used for transferring ownership of internal data ofString
values to otherString
values. If ownership was transferred, then the subject value will be completely invalidated and will be unable to be used afterwards. -
func give() String
Like
commit()
, except requires ownership. Not having ownership will cause a runtime error. -
func first(this *String, character ubyte) long
Returns the index of the first occurrence of a character within a string. Returns
-1
if none exists. -
func first(this *String, sub String) long
Returns the index of the first occurrence of a substring within a string. Returns
-1
if none exists. -
func last(this *String, character ubyte) long
Returns the index of the last occurrence of a character within a string. Returns
-1
if none exists. -
func last(this *String, sub String) long
Returns the index of the last occurrence of a substring within a string. Returns
-1
if none exists. -
func reverse(this *String) void
Reverses a string.
-
func reversed(this *String) String
Returns a reversed copy of a string.
-
func sub(this *String, a, n usize) String
Returns a copy of a section of a string, starting at
a
and going on forn
characters. -
func segment(this *String, a, b usize) String
Returns a copy of a section of a string, starting at
a
and ending atb
characters. -
func span(this *String, a, n usize) StringView
Returns a view of a section of a string, starting at
a
and going on forn
characters. -
func range(this *String, a, b usize) StringView
Returns a view of a section of a string, starting at
a
and ending atb
characters. -
func reduce(this *String, amount usize) void
Removes the last
amount
characters from a string. -
func reduced(this *String, amount usize) String
Returns a copy of a string with the last
amount
characters removed. -
func reducedView(this *String, amount usize) StringView
Returns a view of a string with the last
amount
characters removed. -
func decapitate(this *String, amount usize) void
Removes the first
amount
characters from a string. -
func decapitated(this *String, amount usize) String
Returns a copy of a string with the first
amount
characters removed. -
func decapitatedView(this *String, amount usize) StringView
Returns a view of a string with the first
amount
characters removed. -
func remove(this *String, index usize) void
Removes a character from a string.
-
func remove(this *String, a, n usize) void
Removes a section of characters from a string starting at
a
and going on forn
characters. -
func removed(this *String, index usize) String
Returns a copy of a string with a character removed.
-
func removed(this *String, a, n usize) String
Returns a copy of a string with a section of characters removed starting at
a
and going on forn
characters. -
func omit(this *String, a, b usize) void
Removes a section of characters from a string starting at
a
and ending atb
characters. -
func omitted(this *String, a, b usize) String
Returns a copy of a string with a section of characters removed starting at
a
and ending atb
characters. -
func append(this *String, other String) void
Appends a string to another string.
-
func append(this *String, other ubyte) void
Appends a character to a string.
-
func append(this *String, other *ubyte) void
Appends a c-string to a string.
-
func append(this *String, other $T~__primitive__) void
Converts a primitive value to string and appends it to a string.
-
func appendOnce(this *String, other String) void
Appends a string to a string if the string doesn't already end with it.
-
func prepend(this *String, other String) void
Prepends a string to another string.
-
func prependOnce(this *String, other String) void
Prepends a string to another string if the string doesn't already start with it.
-
func clone(this *String) String
Returns a copy of a string.
-
func make(this *String) void
If a string doesn't have ownership, a copy will be made so that it does.
-
func reference(this *String) String
Returns a string that references the internal data of another string.
-
func replaceOnce(this *String, from ubyte, to String) String
Replaces the first occurrence of
from
withto
in a string. -
func replaceOnce(this *String, from, to String) String
Replaces the first occurrence of
from
withto
in a string. -
func replace(this *String, from ubyte, to String) String
Replaces all occurrences of
from
withto
in a string. -
func replace(this *String, from, to String) String
Replaces all occurrences of
from
withto
in a string. -
func equals(this *String, other POD String) bool
Returns whether two strings are equal.
-
func cstr(this *String) *ubyte
Returns a heap-allocated c-string with the contents of a string.
-
func startsWith(this *String, other String) bool
Returns whether a string starts with the contents of another string.
-
func endsWith(this *String, other String) bool
Returns whether a string ends with the contents of another string.
-
func longestLength(this *String) usize
Returns the longest line length in a string. Lines are separated by
'\n'
characters. -
func count(this *String, character ubyte) usize
Returns the number of occurrences of a certain character within a string.
-
func count(this *String, substring String) usize
Returns the number of occurrences of a substring within a string.
-
func clear(this *String)
Sets a string's length to zero.
-
func clean(this *String)
Deletes the internal contents of a string if it has ownership, and then zero-initializes the string so that it's a fresh untouched string.
-
func empty(this *String) bool
Returns whether a string's
length
is zero. -
func contains(this *String, needle String) bool
Returns whether a string is contained within another string.
-
func contains(this *String, needle ubyte) bool
Returns whether a string contains a certain character.
-
func uppercase(this *String) String
Returns an uppercased copy of a string.
-
func lowercase(this *String) String
Returns an lowercased copy of a string.
-
func trim(this *String) void
Modifies a string to remove leading and trailing whitespace.
-
func trimmed(this *String) String
Returns a copy of a string with leading and trailing whitespace removed.
-
func trimmedView(this *String) StringView
Returns a view of a string with leading and trailing whitespace removed.
#default String_log_deletion false
#default String_error_on_donor_usage true
#default String_include_deprecated_modulus_formatting true
#default String_include_deprecated_equals true