JX Application Framework
|
#include <JString.h>
Public Types | |
enum | ExponentDisplay { kStandardExponent = -10000 , kForceExponent = -9999 , kForceNoExponent = 0 , kUseGivenExponent = 1 } |
enum | Base { kBase10 = 10 , kBase2 = 2 , kBase8 = 8 , kBase16 = 16 } |
enum | Case { kIgnoreCase = 0 , kCompareCase = 1 } |
enum | Copy { kNoCopy = 0 , kCopy = 1 } |
Public Member Functions | |
JString () | |
JString (const JString &str, const Copy copy=kCopy) | |
JString (const JString &str, const JCharacterRange &range, const Copy copy=kCopy) | |
JString (const JUtf8Byte *str, const JSize byteCount, const Copy copy=kCopy) | |
JString (const JUtf8Byte *str, const JUtf8ByteRange &range, const Copy copy=kCopy) | |
JString (const JUtf8Character &c) | |
JString (const std::string &str, const JUtf8ByteRange &range) | |
JString (const bool normalize) | |
JString (const JUtf8Byte *str, const Copy copy=kCopy) | |
JString (const std::string &str) | |
JString (const JUInt64 number, const Base base=kBase10, const bool pad=false) | |
JString (const JFloat number, const JInteger precision=kPrecisionAsNeeded, const ExponentDisplay expDisplay=kStandardExponent, const JInteger exponent=0, const JInteger sigDigitCount=0) | |
~JString () | |
void * | operator new (size_t sz) noexcept |
void * | operator new (size_t size, const JUtf8Byte *file, const JUInt32 line, const int type, const bool assert) noexcept |
void * | operator new (size_t sz, const bool forceShallow) noexcept |
void | operator delete (void *memory) noexcept |
JString & | operator= (const JString &str) |
JString & | operator= (const JUtf8Byte *str) |
JString & | operator= (const std::string &str) |
JString & | operator= (const JUtf8Character &c) |
JString & | operator+= (const JString &str) |
JString & | operator+= (const JUtf8Byte *str) |
JString & | operator+= (const std::string &str) |
JString & | operator+= (const JUtf8Character &c) |
void | Set (const JString &str) |
void | Set (const JString &str, const JCharacterRange &range) |
void | Set (const JUtf8Byte *str) |
void | Set (const JUtf8Byte *str, const JSize byteCount) |
void | Set (const JUtf8Byte *str, const JUtf8ByteRange &range) |
void | Set (const std::string &str) |
void | Set (const std::string &str, const JUtf8ByteRange &range) |
void | Set (const JUtf8Character &c) |
void | Clear () |
bool | IsEmpty () const |
JSize | GetCharacterCount () const |
JSize | GetByteCount () const |
bool | CharacterIndexValid (const JIndex index) const |
bool | ByteIndexValid (const JIndex index) const |
bool | RangeValid (const JCharacterRange &range) const |
bool | RangeValid (const JUtf8ByteRange &range) const |
bool | IsAscii () const |
JUtf8Character | GetFirstCharacter () const |
JUtf8Character | GetLastCharacter () const |
const JUtf8Byte * | GetBytes () const |
JUtf8Byte * | AllocateBytes () const |
bool | IsOwner () const |
const JUtf8Byte * | GetRawBytes () const |
bool | StartsWith (const JString &str, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const JString &str, const JCharacterRange &range, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const JUtf8Byte *str, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const JUtf8Byte *str, const JSize byteCount, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const JUtf8Byte *str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const JUtf8Character &c, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const std::string &str, const Case caseSensitive=kCompareCase) const |
bool | StartsWith (const std::string &str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JString &str, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JString &str, const JCharacterRange &range, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JUtf8Byte *str, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JUtf8Byte *str, const JSize byteCount, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JUtf8Byte *str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
bool | Contains (const JUtf8Character &c, const Case caseSensitive=kCompareCase) const |
bool | Contains (const std::string &str, const Case caseSensitive=kCompareCase) const |
bool | Contains (const std::string &str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JString &str, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JString &str, const JCharacterRange &range, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JUtf8Byte *str, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JUtf8Byte *str, const JSize byteCount, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JUtf8Byte *str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const JUtf8Character &c, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const std::string &str, const Case caseSensitive=kCompareCase) const |
bool | EndsWith (const std::string &str, const JUtf8ByteRange &range, const Case caseSensitive=kCompareCase) const |
JString | EncodeBase64 () const |
bool | DecodeBase64 (JString *str) const |
void | Read (std::istream &input, const JSize byteCount) |
void | ReadDelimited (std::istream &input) |
void | Print (std::ostream &output) const |
void | PrintHex (std::ostream &output) const |
JSize | GetMinLgSize () const |
void | SetMinLgSize (const JSize lgSize) |
void | Prepend (const JString &str) |
void | Prepend (const JUtf8Byte *str) |
void | Prepend (const JUtf8Byte *str, const JSize byteCount) |
void | Prepend (const std::string &str) |
void | Prepend (const JUtf8Character &c) |
void | Append (const JString &str) |
void | Append (const JUtf8Byte *str) |
void | Append (const JUtf8Byte *str, const JSize byteCount) |
void | Append (const std::string &str) |
void | Append (const JUtf8Character &c) |
void | TrimWhitespace () |
void | ToLower () |
void | ToUpper () |
bool | MatchCase (const JString &source, const JCharacterRange &sourceRange) |
bool | MatchCase (const JUtf8Byte *source, const JUtf8ByteRange &sourceRange) |
bool | MatchCase (const std::string &source, const JUtf8ByteRange &sourceRange) |
bool | MatchCase (const JString &source, const JCharacterRange &sourceRange, const JCharacterRange &destRange) |
bool | MatchCase (const JUtf8Byte *source, const JUtf8ByteRange &sourceRange, const JCharacterRange &destRange) |
bool | MatchCase (const std::string &source, const JUtf8ByteRange &sourceRange, const JCharacterRange &destRange) |
void | Split (const JUtf8Byte *separator, JPtrArray< JString > *partList, const JSize limit=0, const bool includeSeparators=false) const |
void | Split (const JString &separator, JPtrArray< JString > *partList, const JSize limit=0, const bool includeSeparators=false) const |
void | Split (const JRegex &separator, JPtrArray< JString > *partList, const JSize limit=0, const bool includeSeparators=false) const |
bool | IsFloat () const |
bool | ConvertToFloat (JFloat *value) const |
bool | IsInteger (const JSize base=10) const |
bool | ConvertToInteger (JInteger *value, const JSize base=10) const |
bool | IsUInt (const JSize base=10) const |
bool | IsHexValue () const |
bool | ConvertToUInt (JUInt *value, const JSize base=10) const |
Static Public Member Functions | |
static bool | IsFloat (const JUtf8Byte *str) |
static bool | IsFloat (const JUtf8Byte *str, const JSize byteCount) |
static bool | ConvertToFloat (const JUtf8Byte *str, JFloat *value) |
static bool | ConvertToFloat (const JUtf8Byte *str, const JSize byteCount, JFloat *value) |
static bool | IsInteger (const JUtf8Byte *str, const JSize base=10) |
static bool | IsInteger (const JUtf8Byte *str, const JSize byteCount, const JSize base) |
static bool | ConvertToInteger (const JUtf8Byte *str, JInteger *value, const JSize base=10) |
static bool | ConvertToInteger (const JUtf8Byte *str, const JSize byteCount, JInteger *value, const JSize base=10) |
static bool | IsUInt (const JUtf8Byte *str, const JSize base=10) |
static bool | IsUInt (const JUtf8Byte *str, const JSize byteCount, const JSize base) |
static bool | IsHexValue (const JUtf8Byte *str) |
static bool | IsHexValue (const JUtf8Byte *str, const JSize byteCount) |
static bool | ConvertToUInt (const JUtf8Byte *str, JUInt *value, const JSize base=10) |
static bool | ConvertToUInt (const JUtf8Byte *str, const JSize byteCount, JUInt *value, const JSize base=10) |
static bool | IsEmpty (const JUtf8Byte *s) |
static bool | IsEmpty (const JString *s) |
static bool | IsValid (const JUtf8Byte *s) |
static bool | IsValid (const JUtf8Byte *s, const JUtf8ByteRange &range) |
static JSize | CountCharacters (const JUtf8Byte *str) |
static JSize | CountCharacters (const JUtf8Byte *str, const JSize byteCount) |
static JSize | CountCharacters (const JUtf8Byte *str, const JUtf8ByteRange &range) |
static JSize | CountBytes (const JUtf8Byte *str, const JSize characterCount) |
static bool | CountBytesBackward (const JUtf8Byte *str, const JSize byteOffset, const JSize characterCount, JSize *byteCount) |
static JUtf8ByteRange | CharacterToUtf8ByteRange (const JUtf8Byte *str, const JCharacterRange &range) |
static int | Compare (const JString &s1, const JString &s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const JString &s1, const JUtf8Byte *s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const JUtf8Byte *s1, const JString &s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const JUtf8Byte *s1, const JUtf8Byte *s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const JString &s1, const std::string &s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const std::string &s1, const JString &s2, const Case caseSensitive=kCompareCase) |
static int | Compare (const JUtf8Byte *s1, const JSize byteCount1, const JUtf8Byte *s2, const JSize byteCount2, const Case caseSensitive=kCompareCase) |
static int | CompareMaxNBytes (const JUtf8Byte *s1, const JUtf8Byte *s2, const JSize N, const Case caseSensitive=kCompareCase) |
static JSize | CalcCharacterMatchLength (const JString &s1, const JString &s2, const Case caseSensitive=kCompareCase) |
static JSize | Normalize (const JUtf8Byte *source, const JSize byteCount, JUtf8Byte **destination) |
static JSize | CopyNormalizedBytes (const JUtf8Byte *source, const JSize maxBytes, JUtf8Byte *destination, const JSize capacity) |
Static Public Attributes | |
static const JInteger | kPrecisionAsNeeded = -1 |
static const JInteger | kDefSigDigitCount = 16 |
static const JString | empty |
static const JString | newline |
static JSize | theDefaultMinLgSize = 5 |
Protected Member Functions | |
const JUtf8Byte * | GetUnterminatedBytes () const |
void | SetIterator (JStringIterator *iter) const |
JUtf8ByteRange | CharacterToUtf8ByteRange (const JCharacterRange &range) const |
bool | SearchForward (const JUtf8Byte *str, const JSize byteCount, const Case caseSensitive, JIndex *byteIndex) const |
bool | SearchBackward (const JUtf8Byte *str, const JSize byteCount, const Case caseSensitive, JIndex *byteIndex) const |
void | ReplaceBytes (const JUtf8ByteRange &replaceRange, const JUtf8Byte *stringToInsert, const JSize insertByteCount) |
bool | MatchCase (const JUtf8Byte *source, const JUtf8ByteRange &sourceRange, const JUtf8ByteRange &destRange) |
Friends | |
class | JStringIterator |
std::istream & | operator>> (std::istream &, JString &) |
std::ostream & | operator<< (std::ostream &, const JString &) |
This class was not designed to be a base class! If you need to override it, be sure to make the destructor virtual. Note that operator== is case sensitive, as one would expect. To avoid the UNIX method of sorting capitalized names separately in front of lowercase names, operator< and operator> are not case sensitive. One should therefore not mix == with < and > when comparing strings. Since stringstream doesn't provide the control we need when converting a number to a string, we use the NumConvert and StrUtil modules. We include them at the end of the file so they are completely hidden and JString is self-contained. By default, JString normalizes all text, so comparing strings is sane. In certain cases, however, this behavior is not desirable, e.g., when the string contains nullptr. The default constructor accepts an optional "normalize" flag to disable normalization. This can only be set in the constructor and cannot be changed afterwards.
enum JString::Base |
enum JString::Case |
enum JString::Copy |
|
inline |
JString::JString | ( | const JString & | str, |
const JCharacterRange & | range, | ||
const Copy | copy = kCopy |
||
) |
JString::JString | ( | const JUtf8Byte * | str, |
const JUtf8ByteRange & | range, | ||
const Copy | copy = kCopy |
||
) |
|
inline |
JString::JString | ( | const std::string & | str, |
const JUtf8ByteRange & | range | ||
) |
|
explicit |
|
inlineexplicit |
|
explicit |
|
explicit |
JString::~JString | ( | ) |
JUtf8Byte * JString::AllocateBytes | ( | ) | const |
This allocates a new pointer, which the caller is responsible for deleting via "delete []".
|
inline |
|
inline |
|
inline |
|
inline |
|
static |
Calculates the number of characters that match from the beginning of the given strings.
CalcMatchLength("abc", "abd") -> 2 CalcMatchLength("abc", "xyz") -> 0 CalcMatchLength("abc", "aBd", false) -> 2
|
inlineprotected |
|
static |
void JString::Clear | ( | ) |
|
inlinestatic |
Replaces strcmp(): + if s1>s2, 0 if s1==s2, - if s1<s2
|
inlinestatic |
|
inlinestatic |
|
static |
Replaces strcmp(): + if s1>s2, 0 if s1==s2, - if s1<s2
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
static |
Replaces strncmp(): + if s1>s2, 0 if s1==s2, - if s1<s2
|
inline |
|
inline |
|
inline |
|
inline |
bool JString::Contains | ( | const JUtf8Byte * | str, |
const JUtf8ByteRange & | range, | ||
const Case | caseSensitive = kCompareCase |
||
) | const |
|
inline |
|
inline |
|
inline |
|
static |
We cannot provide a public interface that takes JUtf8Byte* + byteCount because strto*() does not provide this interface. We use it internally as an optimization since JStrings know their length.
If we tried to provide the JUtf8Byte* + byteCount interface, we could not stop strto*() from gobbling digits beyond byteCount, which would obviously be an error. Convert string to a floating point value. Returns true if successful.
This function accepts hex values like 0x2A.
|
static |
Convert string to a signed integer. Returns true if successful.
base must be between 2 and 36 inclusive. If the string begins with 0x or 0X, base is forced to 16.
|
inlinestatic |
|
static |
Convert string to an unsigned integer. Returns true if successful.
base must be between 2 and 36 inclusive. If the string begins with 0x or 0X, base is forced to 16.
|
inlinestatic |
|
static |
This function processes at most maxBytes from source and places the normalized characters into destination. (Normalization greatly simplifies all further operations on the string.)
The return value is the number of bytes inserted into destination. Because of normalization, this may be less than the number of input bytes processed.
destination is guaranteed to be nullptr terminated. The name capacity should remind you that there must also be room for a null terminator - at most capacity-1 bytes are actually inserted.
|
static |
Works backwards from the given offset. Returns false if it prematurely hits the start of the string.
|
static |
JString JString::EncodeBase64 | ( | ) | const |
|
inline |
|
inline |
|
inline |
|
inline |
bool JString::EndsWith | ( | const JUtf8Byte * | str, |
const JUtf8ByteRange & | range, | ||
const Case | caseSensitive = kCompareCase |
||
) | const |
|
inline |
|
inline |
|
inline |
|
inline |
const JUtf8Byte * JString::GetBytes | ( | ) | const |
|
inline |
JUtf8Character JString::GetFirstCharacter | ( | ) | const |
JUtf8Character JString::GetLastCharacter | ( | ) | const |
|
inline |
|
inline |
NOT guaranteed to be nullptr terminated
|
inlineprotected |
|
inline |
|
inline |
|
inline |
Returns true if we can convert ourselves to a number.
|
inline |
Returns true if string begins with whitespace+"0x".
|
inlinestatic |
|
inline |
|
inlinestatic |
|
static |
bool JString::MatchCase | ( | const JString & | source, |
const JCharacterRange & | sourceRange | ||
) |
bool JString::MatchCase | ( | const JString & | source, |
const JCharacterRange & | sourceRange, | ||
const JCharacterRange & | destRange | ||
) |
bool JString::MatchCase | ( | const JUtf8Byte * | source, |
const JUtf8ByteRange & | sourceRange | ||
) |
bool JString::MatchCase | ( | const JUtf8Byte * | source, |
const JUtf8ByteRange & | sourceRange, | ||
const JCharacterRange & | destRange | ||
) |
|
protected |
Adjusts the case of destRange to match the case of sourceRange in source. Returns true if any changes were made.
If sourceRange and destRange have the same number of characters, we match the case of each character individually.
Otherwise:
If both first characters are letters, the first letter of destRange is adjusted to the same case as the first character of sourceRange.
If the rest of sourceRange contains at least one alphabetic character and all its alphabetic characters have the same case, all the alphabetic characters in destRange are coerced to that case.
bool JString::MatchCase | ( | const std::string & | source, |
const JUtf8ByteRange & | sourceRange | ||
) |
bool JString::MatchCase | ( | const std::string & | source, |
const JUtf8ByteRange & | sourceRange, | ||
const JCharacterRange & | destRange | ||
) |
|
static |
Allocates space and returns the normalized version of the input characters.
The caller must delete [] the result!
|
noexcept |
|
noexcept |
|
noexcept |
If a JString is constructed on the heap, we need to cancel out the Name Return Value Optimization to ensure that we own the data.
|
noexcept |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
void JString::Print | ( | std::ostream & | output | ) | const |
Display the text in such a way that the user can understand it. (i.e. don't display it in quotes, and don't use the internal ")
void JString::PrintHex | ( | std::ostream & | output | ) | const |
Display the hex bytes.
|
inline |
|
inline |
void JString::Read | ( | std::istream & | input, |
const JSize | count | ||
) |
Read the specified number of characters from the stream. This replaces the current contents of the string.
void JString::ReadDelimited | ( | std::istream & | input | ) |
Read a string of quote delimited characters from the stream. Double quote (") and backslash () characters that are part of the string must be preceded by a backslash. This replaces the current contents of the string.
|
protected |
Replace the specified range with the given bytes.
It is safe to pass in "nullptr, 0" for the insertion, to do a remove.
|
protected |
Return the byte index corresponding to the start of the previous occurrence of the given sequence in our string, starting at *byteIndex.
In: *byteIndex is first location to consider Out: If function returns true, *byteIndex is location of prev occurrence. Otherwise, *byteIndex is zero.
|
protected |
Return the byte index corresponding to the start of the next occurrence of the given sequence in our string, starting at *byteIndex.
In: *byteIndex is first location to consider Out: If function returns true, *byteIndex is location of next occurrence. Otherwise, *byteIndex is beyond end of string.
void JString::Set | ( | const JString & | str | ) |
void JString::Set | ( | const JString & | str, |
const JCharacterRange & | range | ||
) |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
protected |
|
inline |
void JString::Split | ( | const JRegex & | separator, |
JPtrArray< JString > * | partList, | ||
const JSize | limit = 0 , |
||
const bool | includeSeparators = false |
||
) | const |
void JString::Split | ( | const JString & | separator, |
JPtrArray< JString > * | partList, | ||
const JSize | limit = 0 , |
||
const bool | includeSeparators = false |
||
) | const |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
bool JString::StartsWith | ( | const JUtf8Byte * | str, |
const JUtf8ByteRange & | range, | ||
const Case | caseSensitive = kCompareCase |
||
) | const |
|
inline |
|
inline |
|
inline |
void JString::ToLower | ( | ) |
Convert all characters to lower case.
void JString::ToUpper | ( | ) |
Convert all characters to upper case.
void JString::TrimWhitespace | ( | ) |
Trim leading and trailing whitespace from ourselves.
TODO: utf8
|
friend |
|
friend |
|
friend |
The string data is delimited by double quotes: "this is a string".
To include double quotes in a string, use " To include a backslash in a string, use \
An exception is made if the streams are std::cin or std::cout. For input, characters are simply read until 'return' is pressed. For output, Print() is used.
|
static |
|
static |
|
static |
|
static |
|
static |