16-bit wide-character string class More...
Typedefs | |
typedef typedefCGUL_BEGIN_C struct cgul_hstring * | cgul_hstring_t |
Simple 16-bit wide-character string class. Unlike the cgul_string
class, this class and cgul_wstring
co-opt the role of wchar_t
strings in C because, as a practical matter, wchar_t
is not portable.
As a result, this class defines cgul_hchar_t
to always be an unsigned 16-bit value. This means you cannot portably use any of the standard C functions with these strings, but you can convert these strings to UTF-8 and use the standard char*
functions.
If you need to read these strings from file, I recommend that the file be UTF-8 encoded. You can then read the strings from file using cgul_crlf_file
. You can then convert them from UTF-8 using cgul_unicode__mbstohcs()
.
If you need to write these strings to file, I recommend that you convert the string to UTF-8 using cgul_unicode__hcstombs()
. You can then write the resulting char*
string to file using cgul_fprintf()
.
typedef typedefCGUL_BEGIN_C struct cgul_hstring* cgul_hstring_t |
Opaque pointer to cgul_hstring
instance.
CGUL_EXPORT cgul_hstring_t cgul_hstring__new | ( | cgul_exception_t * | cex | ) |
Create a new cgul_hstring
object that holds an empty string. The caller is responsible for freeing the object by calling cgul_hstring__delete()
. If memory cannot be allocated, NULL
is returned, and an exception is thrown.
The following three methods implement the "cacheable" interface defined by cgul_cache
:
cgul_hstring__new() cgul_hstring__delete() cgul_hstring__clear()
[in,out] | cex | c-style exception |
cgul_hstring
instance Referenced by cgul_hstring_cxx::cgul_hstring_cxx().
CGUL_EXPORT cgul_hstring_t cgul_hstring__new_from_hcstring | ( | cgul_exception_t * | cex, |
const cgul_hchar_t * | value | ||
) |
Create a new cgul_hstring
object that is initialized with a copy of value
. The caller is responsible for freeing the object by calling cgul_hstring__delete()
. If memory cannot be allocated, NULL
is returned, and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | value | initial value |
cgul_hstring
instance Referenced by cgul_hstring_cxx::cgul_hstring_cxx().
CGUL_EXPORT void cgul_hstring__delete | ( | cgul_hstring_t | hs | ) |
cgul_hstring__delete() frees all internally allocated memory. Do not try to use hs
after calling this method.
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::set_obj(), and cgul_hstring_cxx::~cgul_hstring_cxx().
CGUL_EXPORT cgul_hchar_t* cgul_hstring__get_value | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
This method returns a pointer to the internal wide-character string. You are allowed to manually set individual characters in the string. Just remember to call cgul_hstring__externally_adjusted()
afterward. Compare this method to cgul_hstring__take_value()
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::get_value(), and cgul_hstring_cxx::operator[]().
CGUL_EXPORT cgul_hchar_t* cgul_hstring__take_value | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
This method lets you take ownership of the underlying wide-character string which is returned. Immediately upon taking ownership, the internal state of this class is reset including resetting a new underlying wide-character string to be empty. One use of this function is to build up a string using cgul_hstring
that you then pass to cgul_hash
or cgul_rbtree
. Compare this function with cgul_hstring__get_value()
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::take_value().
CGUL_EXPORT void cgul_hstring__set_value | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
const cgul_hchar_t * | value | ||
) |
Set the value of the underlying wide-character string by copying characters from value
to s
. The caller is responsible for making sure value
is terminated with a CGUL_HCHAR__NUL
character. This method does not uses strcpy()
or memcpy()
. This means that value
and s
can overlap.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | value | new value |
Referenced by cgul_hstring_cxx::operator=(), and cgul_hstring_cxx::set_value().
CGUL_EXPORT cgul_hchar_t cgul_hstring__get_value_at | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | index | ||
) |
This method returns the character in hs
indicated by index
where index
is zero-based. If index
is out of bounds, (cgul_hchar_t)0
is returned and an exception is thrown.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | index | index into hs |
index
Referenced by cgul_hstring_cxx::get_value_at().
CGUL_EXPORT void cgul_hstring__set_value_at | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | index, | ||
cgul_hchar_t | hc | ||
) |
This method sets the wide character in hs
indicated by index
where index
is zero-based to hc
. If index
is out of bounds, an exception is thrown.
[in] | cex | c-style exception |
[in] | hs | cgul_string instance |
[in] | index | index into s |
[in] | hc | character |
CGUL_EXPORT void cgul_hstring__clear | ( | cgul_hstring_t | hs | ) |
This method is equivalent to calling cgul_hstring__set_value()
where value
is an empty string. This method is designed to be used as the cgul_cache_restructor_t
method. This means it is not allowed to throw an exception.
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::clear().
CGUL_EXPORT signed char cgul_hstring__to_schar | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to a signed char
. Typically, "base" will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading character in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_schar().
CGUL_EXPORT unsigned char cgul_hstring__to_uchar | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to an unsigned char
. Typically, base
will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading characters in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_uchar().
CGUL_EXPORT short int cgul_hstring__to_short | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to a short
. Typically, "base" will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading character in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_short().
CGUL_EXPORT unsigned short int cgul_hstring__to_ushort | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to an unsigned short
. Typically, base
will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading characters in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_ushort().
CGUL_EXPORT int cgul_hstring__to_int | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to an int
. Typically, "base" will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading character in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_int().
CGUL_EXPORT unsigned int cgul_hstring__to_unsigned | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to an unsigned
. Typically, base
will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading characters in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_unsigned().
CGUL_EXPORT long int cgul_hstring__to_long | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to a long
. Typically, base
will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading characters in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_long().
CGUL_EXPORT unsigned long int cgul_hstring__to_unsigned_long | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
int | base | ||
) |
This method converts the string to an unsigned long
. Typically, base
will be set to 0 to allow conversion for decimal, hexadecimal, and octal based on the leading characters in the string. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | base | base to use for the conversion |
Referenced by cgul_hstring_cxx::to_unsigned_long().
CGUL_EXPORT double cgul_hstring__to_double | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
This method converts the string to a double
. The string cannot have any extraneous characters. If it does, the conversion will fail. On success, the converted value is returned; otherwise, 0.0 is returned and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::to_double().
CGUL_EXPORT void cgul_hstring__append | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
const cgul_hchar_t * | value | ||
) |
Appends value
to hs
. The implementation of this method is such that it is possible for value
to be a substring of hs
. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | value | value to append |
Referenced by cgul_hstring_cxx::append(), and cgul_hstring_cxx::operator+=().
CGUL_EXPORT void cgul_hstring__append_block | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
const void * | block, | ||
size_t | block_size | ||
) |
Append a block of non-CGUL_HCHAR__NUL
characters. The block_size
is the number of characters (not bytes) in the block. The caller should verify that there are no embedded NUL characters in the string. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | block | block to append |
[in] | block_size | size of block to append |
Referenced by cgul_hstring_cxx::append_block().
CGUL_EXPORT void cgul_hstring__append_char | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
cgul_hchar_t | hc | ||
) |
Append hc
to hs
.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | hc | wide character to append |
Referenced by cgul_hstring_cxx::append_char().
CGUL_EXPORT void cgul_hstring__insert | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | index, | ||
const cgul_hchar_t * | value, | ||
size_t | value_length | ||
) |
Inserts value
into hs
at index
which can range from 0 to the length (inclusive) of hs
. If index
is out of bounds or if memory cannot be allocated, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | index | index in s where value is inserted |
[in] | value | value to insert |
[in] | value_length | length of value |
Referenced by cgul_hstring_cxx::insert().
CGUL_EXPORT void cgul_hstring__insert_char | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | index, | ||
cgul_hchar_t | value | ||
) |
Inserts value
into hs
at index
which can range from 0 to the length (inclusive) of hs
. If index
is out of bounds or if memory cannot be allocated, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | index | index in s where value is inserted |
[in] | value | value to insert |
Referenced by cgul_hstring_cxx::insert_char().
CGUL_EXPORT void cgul_hstring__pad_right | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | width, | ||
cgul_hchar_t | pad | ||
) |
Pad the current string in hs
such that it is at least width
characters wide using the pad
character. hs
will be right aligned.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | width | padding width |
[in] | pad | wide character to use for padding |
Referenced by cgul_hstring_cxx::pad_right().
CGUL_EXPORT void cgul_hstring__pad_left | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | width, | ||
cgul_hchar_t | pad | ||
) |
Pad the current string in hs
such that it is at least width
characters wide using the pad
character. hs
will be left aligned.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | width | padding width |
[in] | pad | wide character to use for padding |
Referenced by cgul_hstring_cxx::pad_left().
CGUL_EXPORT void cgul_hstring__trim | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Trim the string by removing leading and trailing white-space where white-space is definded by cgul_hchar__isspace()
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::trim().
CGUL_EXPORT void cgul_hstring__trim_left | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Trim the string by removing leading white-space where white-space is definded by cgul_hchar__isspace()
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::trim_left().
CGUL_EXPORT void cgul_hstring__trim_right | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Trim the string by removing trailing white-space where white-space is definded by cgul_hchar__isspace()
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::trim_right().
CGUL_EXPORT void cgul_hstring__reverse | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Reverse the order of the characaters in hs
.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::reverse().
CGUL_EXPORT size_t cgul_hstring__get_length | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
This method returns the length of the string. This is the number of cgul_hchar_t
elements in the string, not the number of bytes. If hs
is a UTF-16 string, surrogate pairs count as two while all other characters count as one. Call cgul_unicode__get_hchar_count()
to get an exact character count for UTF-16 strings.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::get_length(), and cgul_hstring_cxx::operator[]().
CGUL_EXPORT size_t cgul_hstring__set_length | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | length | ||
) |
This method can be used to set the length of hs
. It can only be used to make hs
shorter. If it is used in an attempt to make hs
longer, an exception is thrown and zero is returned. If the length of hs
is successfully changed, the previous length of the string is returned.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | length | new, shorter length of the string |
Referenced by cgul_hstring_cxx::set_length().
CGUL_EXPORT void cgul_hstring__externally_adjusted | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
If you manually alter the underlying buffer by modifying the cgul_hchar_t*
string returned by cgul_hstring__get_value()
, you'll need to manually have the class reset its internal state.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::externally_adjusted().
CGUL_EXPORT void cgul_hstring__trim_capacity | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Trim the string by returning excess capacity to the heap.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::trim_capacity().
CGUL_EXPORT size_t cgul_hstring__get_minimum_capacity | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs | ||
) |
Get the minimum capacity.
[in] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
Referenced by cgul_hstring_cxx::get_minimum_capacity().
CGUL_EXPORT void cgul_hstring__set_minimum_capacity | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs, | ||
size_t | minimum_capacity | ||
) |
Set the minimum capacity. The default value is 128
which means there is always capacity for 127 characters plus one NUL-terminator. Decreasing the capacity will decrease the amount of space used and (typically) also decrease performance. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | hs | cgul_hstring instance |
[in] | minimum_capacity | minimum capacity |
Referenced by cgul_hstring_cxx::set_minimum_capacity().
CGUL_EXPORT int cgul_hstring__compare | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs1, | ||
cgul_hstring_t | hs2 | ||
) |
Perform a case-significant wide-character string comparison of hs1
and hs2
. Return -1, 0, or 1 if hs1
is less than, equal to, or greater than hs2
respectively.
For simplicity, this function compares strings stricly by the ordinal value of each character which should be sufficient, for example, when inserting entire cgul_hstring
objects as keys into a cgul_rbtree
object; however, the comparison results are not likely to be the same as what strcoll()
would return if used on the UTF-8 version of the strings.
cgul_hchar__hcscmp()
instead if inserting raw cgul_hchar
strings as keys into a cgul_rbtree
object. [in] | cex | c-style exception |
[in] | hs1 | left-hand side |
[in] | hs2 | right-hand side |
hs1
is less than, equal to, or greater than hs2
respectively Referenced by cgul_hstring_cxx::compare().
CGUL_EXPORT void cgul_hstring__swap | ( | cgul_exception_t * | cex, |
cgul_hstring_t | hs1, | ||
cgul_hstring_t | hs2 | ||
) |
Swap the underlying data for hs1
and hs2
. For large strings, this should be much faster than trying to do the same thing using cgul_hstring__set_value()
.
[in] | cex | c-style exception |
[in] | hs1 | cgul_hstring instance |
[in] | hs2 | cgul_hstring instance |
Referenced by cgul_hstring_cxx::swap().