-
-
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) voidfunc __pass__(string POD String) Stringfunc __assign__(this *String, other POD String) voidfunc __access__(this *String, index usize) *ubytefunc __equals__(lhs String, rhs String) boolfunc __not_equals__(lhs String, rhs String) boolfunc __add__(lhs String, rhs String) Stringfunc __add__(lhs String, rhs ubyte) Stringfunc __add__(lhs POD String, rhs $T~__primitive__) Stringfunc __multiply__(lhs String, times int) Stringfunc __modulus__(lhs POD String, rhs String) Stringfunc __modulus__(lhs POD String, rhs int) Stringfunc __modulus__(lhs POD String, rhs ptr) Stringfunc __modulus__(lhs POD String, rhs bool) Stringfunc __modulus__(lhs POD String, rhs $T~__primitive__) Stringfunc __less_than__(lhs String, rhs String) boolfunc __greater_than__(lhs String, rhs String) boolfunc __less_than_or_equal__(lhs String, rhs String) boolfunc __greater_than_or_equal__(lhs String, rhs String) boolfunc __array__(this *String) *ubytefunc __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 deleted |
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
PODwill 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) StringCreates a
Stringvalue from a null-terminated string. The returned value will have ownership of a copy of the string. -
func String(array *ubyte, length usize) StringCreates a
Stringvalue from a character array. The returned value will have ownership of a copy of the string. -
func StringView(null_terminated *ubyte) StringCreates a
Stringvalue from a null-terminated string. The returned value will be a view of the memory supplied. -
func StringView(array *ubyte, length usize) StringCreates a
Stringvalue from a character array. The returned value will be a view of the memory supplied. -
func stringConstant(null_terminated *ubyte) StringCreates a
Stringvalue from a null-terminated string. The returned value will reference the memory supplied. -
func stringTake(null_terminated *ubyte) StringCreates a
Stringvalue 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) StringCreates a
Stringvalue 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) boolReturns whether a string is contained within another string.
-
func contains(haystack String, needle ubyte) boolReturns whether a string contains a certain character.
-
func startsWith(string String, other POD String) boolReturns whether a string starts with the contents of another string.
-
func endsWith(string String, other POD String) boolReturns whether a string ends with the contents of another string.
-
func longestLength(string String) usizeReturns the longest line length in a string. Lines are separated by
'\n'characters. -
func count(string String, character ubyte) usizeReturns the number of occurrences of a certain character within a string.
-
func count(string String, substring String) usizeReturns the number of occurrences of a substring within a string.
-
func uppercase(string String) StringReturns an uppercased copy of a string.
-
func lowercase(string String) StringReturns a lowercased copy of a string.
-
func trim(string String) StringReturns a trimmed copy of a string.
-
func toString(value POD String) StringIdentity function that returns the supplied string.
-
func compare(this *String, other String) intCompares two strings.
- Returns
-1ifthisshould come first alphabetically. - Returns
1ifthisshould come second alphabetically. - Returns
0ifthisis equal toother.
- Returns
-
func commit(this *String) StringChanges the ownership of a
Stringto beStringOwnership::REFERENCE. If theStringpreviously had ownership, then aStringvalue will be returned withStringOwnership::GIVEN, otherwise the originalStringwill be returned. This method is used for transferring ownership of internal data ofStringvalues to otherStringvalues. -
func donate(this *String) StringIf the
Stringhas ownership, then the subject's ownership will be changed toStringOwnership::DONORand aStringvalue will be returned withStringOwnership::GIVEN, otherwise the originalStringwill be returned. This method is used for transferring ownership of internal data ofStringvalues to otherStringvalues. If ownership was transferred, then the subject value will be completely invalidated and will be unable to be used afterwards. -
func give() StringLike
commit(), except requires ownership. Not having ownership will cause a runtime error. -
func first(this *String, character ubyte) longReturns the index of the first occurrence of a character within a string. Returns
-1if none exists. -
func first(this *String, sub String) longReturns the index of the first occurrence of a substring within a string. Returns
-1if none exists. -
func last(this *String, character ubyte) longReturns the index of the last occurrence of a character within a string. Returns
-1if none exists. -
func last(this *String, sub String) longReturns the index of the last occurrence of a substring within a string. Returns
-1if none exists. -
func reverse(this *String) voidReverses a string.
-
func reversed(this *String) StringReturns a reversed copy of a string.
-
func sub(this *String, a, n usize) StringReturns a copy of a section of a string, starting at
aand going on forncharacters. -
func segment(this *String, a, b usize) StringReturns a copy of a section of a string, starting at
aand ending atbcharacters. -
func span(this *String, a, n usize) StringViewReturns a view of a section of a string, starting at
aand going on forncharacters. -
func range(this *String, a, b usize) StringViewReturns a view of a section of a string, starting at
aand ending atbcharacters. -
func reduce(this *String, amount usize) voidRemoves the last
amountcharacters from a string. -
func reduced(this *String, amount usize) StringReturns a copy of a string with the last
amountcharacters removed. -
func reducedView(this *String, amount usize) StringViewReturns a view of a string with the last
amountcharacters removed. -
func decapitate(this *String, amount usize) voidRemoves the first
amountcharacters from a string. -
func decapitated(this *String, amount usize) StringReturns a copy of a string with the first
amountcharacters removed. -
func decapitatedView(this *String, amount usize) StringViewReturns a view of a string with the first
amountcharacters removed. -
func remove(this *String, index usize) voidRemoves a character from a string.
-
func remove(this *String, a, n usize) voidRemoves a section of characters from a string starting at
aand going on forncharacters. -
func removed(this *String, index usize) StringReturns a copy of a string with a character removed.
-
func removed(this *String, a, n usize) StringReturns a copy of a string with a section of characters removed starting at
aand going on forncharacters. -
func omit(this *String, a, b usize) voidRemoves a section of characters from a string starting at
aand ending atbcharacters. -
func omitted(this *String, a, b usize) StringReturns a copy of a string with a section of characters removed starting at
aand ending atbcharacters. -
func append(this *String, other String) voidAppends a string to another string.
-
func append(this *String, other ubyte) voidAppends a character to a string.
-
func append(this *String, other *ubyte) voidAppends a c-string to a string.
-
func append(this *String, other $T~__primitive__) voidConverts a primitive value to string and appends it to a string.
-
func appendOnce(this *String, other String) voidAppends a string to a string if the string doesn't already end with it.
-
func prepend(this *String, other String) voidPrepends a string to another string.
-
func prependOnce(this *String, other String) voidPrepends a string to another string if the string doesn't already start with it.
-
func clone(this *String) StringReturns a copy of a string.
-
func make(this *String) voidIf a string doesn't have ownership, a copy will be made so that it does.
-
func reference(this *String) StringReturns a string that references the internal data of another string.
-
func replaceOnce(this *String, from ubyte, to String) StringReplaces the first occurrence of
fromwithtoin a string. -
func replaceOnce(this *String, from, to String) StringReplaces the first occurrence of
fromwithtoin a string. -
func replace(this *String, from ubyte, to String) StringReplaces all occurrences of
fromwithtoin a string. -
func replace(this *String, from, to String) StringReplaces all occurrences of
fromwithtoin a string. -
func equals(this *String, other POD String) boolReturns whether two strings are equal.
-
func cstr(this *String) *ubyteReturns a heap-allocated c-string with the contents of a string.
-
func startsWith(this *String, other String) boolReturns whether a string starts with the contents of another string.
-
func endsWith(this *String, other String) boolReturns whether a string ends with the contents of another string.
-
func longestLength(this *String) usizeReturns the longest line length in a string. Lines are separated by
'\n'characters. -
func count(this *String, character ubyte) usizeReturns the number of occurrences of a certain character within a string.
-
func count(this *String, substring String) usizeReturns 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) boolReturns whether a string's
lengthis zero. -
func contains(this *String, needle String) boolReturns whether a string is contained within another string.
-
func contains(this *String, needle ubyte) boolReturns whether a string contains a certain character.
-
func uppercase(this *String) StringReturns an uppercased copy of a string.
-
func lowercase(this *String) StringReturns an lowercased copy of a string.
-
func trim(this *String) voidModifies a string to remove leading and trailing whitespace.
-
func trimmed(this *String) StringReturns a copy of a string with leading and trailing whitespace removed.
-
func trimmedView(this *String) StringViewReturns 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