Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vcodepoint_h 00009 #define vcodepoint_h 00010 00013 #include "vtypes.h" 00014 00015 class VChar; 00016 class VString; 00017 class VBinaryIOStream; 00018 class VTextIOStream; 00019 00034 class VCodePoint { 00035 00036 public: 00037 00043 explicit VCodePoint(int i); 00050 explicit VCodePoint(char c); 00057 explicit VCodePoint(const VChar& c); 00065 explicit VCodePoint(const VString& hexNotation); 00073 explicit VCodePoint(const Vu8* buffer, int startOffset); 00081 explicit VCodePoint(VBinaryIOStream& stream); 00089 explicit VCodePoint(VTextIOStream& utf8Stream); 00099 explicit VCodePoint(const std::wstring& utf16WideString, int atIndex); 00100 00105 int getUTF8Length() const { return mUTF8Length; } 00110 int getUTF16Length() const { return mUTF16Length; } 00115 int intValue() const { return mIntValue; } 00122 VString toString() const; 00130 VChar toASCIIChar() const; 00138 std::wstring toUTF16WideString() const; 00143 void writeToBinaryStream(VBinaryIOStream& stream) const; 00150 bool isNull() const { return mIntValue == 0; } 00155 bool isNotNull() const { return mIntValue != 0; } 00163 bool isASCII() const { return mUTF8Length == 1; } 00167 bool isWhitespace() const; 00168 bool isAlpha() const; 00169 bool isNumeric() const; 00170 bool isAlphaNumeric() const; 00171 bool isHexadecimal() const; 00172 00176 friend inline bool operator==(const VCodePoint& p1, const VCodePoint& p2); 00177 00178 // Helper utility functions for dealing with UTF-8 buffers in VString and VStringIterator: 00179 00186 static int getUTF8LengthFromUTF8StartByte(Vu8 startByte); 00192 static int getUTF8LengthFromCodePointValue(int intValue); 00199 static bool isUTF8ContinuationByte(Vu8 byteValue); 00206 static int countUTF8CodePoints(const Vu8* buffer, int numBytes); 00218 static int getPreviousUTF8CodePointOffset(const Vu8* buffer, int offset); 00219 00225 static int getUTF16LengthFromCodePointValue(int intValue); 00234 static bool isUTF16SurrogateCodeUnit(wchar_t codeUnit); 00235 00236 private: 00237 00248 void _initFromUTF8Bytes(int numBytesToUse, Vu8 byte0, Vu8 byte1, Vu8 byte2, Vu8 byte3); 00249 00259 void _initFromUTF16Surrogates(wchar_t leadSurrogate, wchar_t trailSurrogate); 00260 00261 int mIntValue; 00262 int mUTF8Length; 00263 int mUTF16Length; 00264 00265 friend inline bool operator==(const VCodePoint& cp, char c); 00266 friend inline bool operator==(char c, const VCodePoint& cp); 00267 }; 00268 00269 inline bool operator==(const VCodePoint& p1, const VCodePoint& p2) { return p1.mIntValue == p2.mIntValue; } 00270 inline bool operator==(const VCodePoint& cp, char c) { return cp.mIntValue == (int) c; } 00271 inline bool operator==(char c, const VCodePoint& cp) { return cp.mIntValue == (int) c; } 00272 00273 #endif /* vcodepoint_h */