michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsUnicharUtils_h__ michael@0: #define nsUnicharUtils_h__ michael@0: michael@0: #include "nsStringGlue.h" michael@0: michael@0: /* (0x3131u <= (u) && (u) <= 0x318eu) => Hangul Compatibility Jamo */ michael@0: /* (0xac00u <= (u) && (u) <= 0xd7a3u) => Hangul Syllables */ michael@0: #define IS_CJ_CHAR(u) \ michael@0: ((0x2e80u <= (u) && (u) <= 0x312fu) || \ michael@0: (0x3190u <= (u) && (u) <= 0xabffu) || \ michael@0: (0xf900u <= (u) && (u) <= 0xfaffu) || \ michael@0: (0xff00u <= (u) && (u) <= 0xffefu) ) michael@0: michael@0: void ToLowerCase(nsAString&); michael@0: void ToUpperCase(nsAString&); michael@0: michael@0: void ToLowerCase(const nsAString& aSource, nsAString& aDest); michael@0: void ToUpperCase(const nsAString& aSource, nsAString& aDest); michael@0: michael@0: uint32_t ToLowerCase(uint32_t); michael@0: uint32_t ToUpperCase(uint32_t); michael@0: uint32_t ToTitleCase(uint32_t); michael@0: michael@0: void ToLowerCase(const char16_t*, char16_t*, uint32_t); michael@0: void ToUpperCase(const char16_t*, char16_t*, uint32_t); michael@0: michael@0: inline bool IsUpperCase(uint32_t c) { michael@0: return ToLowerCase(c) != c; michael@0: } michael@0: michael@0: inline bool IsLowerCase(uint32_t c) { michael@0: return ToUpperCase(c) != c; michael@0: } michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: michael@0: class nsCaseInsensitiveStringComparator : public nsStringComparator michael@0: { michael@0: public: michael@0: virtual int32_t operator() (const char16_t*, michael@0: const char16_t*, michael@0: uint32_t, michael@0: uint32_t) const; michael@0: }; michael@0: michael@0: class nsCaseInsensitiveUTF8StringComparator : public nsCStringComparator michael@0: { michael@0: public: michael@0: virtual int32_t operator() (const char*, michael@0: const char*, michael@0: uint32_t, michael@0: uint32_t) const; michael@0: }; michael@0: michael@0: class nsCaseInsensitiveStringArrayComparator michael@0: { michael@0: public: michael@0: template michael@0: bool Equals(const A& a, const B& b) const { michael@0: return a.Equals(b, nsCaseInsensitiveStringComparator()); michael@0: } michael@0: }; michael@0: michael@0: class nsASCIICaseInsensitiveStringComparator : public nsStringComparator michael@0: { michael@0: public: michael@0: nsASCIICaseInsensitiveStringComparator() {} michael@0: virtual int operator() (const char16_t*, michael@0: const char16_t*, michael@0: uint32_t, michael@0: uint32_t) const; michael@0: }; michael@0: michael@0: inline bool michael@0: CaseInsensitiveFindInReadable(const nsAString& aPattern, michael@0: nsAString::const_iterator& aSearchStart, michael@0: nsAString::const_iterator& aSearchEnd) michael@0: { michael@0: return FindInReadable(aPattern, aSearchStart, aSearchEnd, michael@0: nsCaseInsensitiveStringComparator()); michael@0: } michael@0: michael@0: inline bool michael@0: CaseInsensitiveFindInReadable(const nsAString& aPattern, michael@0: const nsAString& aHay) michael@0: { michael@0: nsAString::const_iterator searchBegin, searchEnd; michael@0: return FindInReadable(aPattern, aHay.BeginReading(searchBegin), michael@0: aHay.EndReading(searchEnd), michael@0: nsCaseInsensitiveStringComparator()); michael@0: } michael@0: michael@0: #endif // MOZILLA_INTERNAL_API michael@0: michael@0: int32_t michael@0: CaseInsensitiveCompare(const char16_t *a, const char16_t *b, uint32_t len); michael@0: michael@0: int32_t michael@0: CaseInsensitiveCompare(const char* aLeft, const char* aRight, michael@0: uint32_t aLeftBytes, uint32_t aRightBytes); michael@0: michael@0: /** michael@0: * This function determines whether the UTF-8 sequence pointed to by aLeft is michael@0: * case-insensitively-equal to the UTF-8 sequence pointed to by aRight. michael@0: * michael@0: * aLeftEnd marks the first memory location past aLeft that is not part of michael@0: * aLeft; aRightEnd similarly marks the end of aRight. michael@0: * michael@0: * The function assumes that aLeft < aLeftEnd and aRight < aRightEnd. michael@0: * michael@0: * The function stores the addresses of the next characters in the sequence michael@0: * into aLeftNext and aRightNext. It's up to the caller to make sure that the michael@0: * returned pointers are valid -- i.e. the function may return aLeftNext >= michael@0: * aLeftEnd or aRightNext >= aRightEnd. michael@0: * michael@0: * If the function encounters invalid text, it sets aErr to true and returns michael@0: * false, possibly leaving aLeftNext and aRightNext uninitialized. If the michael@0: * function returns true, aErr is guaranteed to be false and both aLeftNext and michael@0: * aRightNext are guaranteed to be initialized. michael@0: */ michael@0: bool michael@0: CaseInsensitiveUTF8CharsEqual(const char* aLeft, const char* aRight, michael@0: const char* aLeftEnd, const char* aRightEnd, michael@0: const char** aLeftNext, const char** aRightNext, michael@0: bool* aErr); michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Hash a UTF8 string as though it were a UTF16 string. michael@0: * michael@0: * The value returned is the same as if we converted the string to UTF16 and michael@0: * then ran HashString() on the result. michael@0: * michael@0: * The given |length| is in bytes. michael@0: */ michael@0: uint32_t michael@0: HashUTF8AsUTF16(const char* aUTF8, uint32_t aLength, bool* aErr); michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* nsUnicharUtils_h__ */