TextUtils.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Character/text operations. */
  6. #ifndef mozilla_TextUtils_h
  7. #define mozilla_TextUtils_h
  8. #include "mozilla/TypeTraits.h"
  9. namespace mozilla {
  10. namespace detail {
  11. template<typename Char>
  12. class MakeUnsignedChar
  13. : public MakeUnsigned<Char>
  14. {};
  15. template<>
  16. class MakeUnsignedChar<char16_t>
  17. {
  18. public:
  19. using Type = char16_t;
  20. };
  21. template<>
  22. class MakeUnsignedChar<char32_t>
  23. {
  24. public:
  25. using Type = char32_t;
  26. };
  27. } // namespace detail
  28. /**
  29. * Returns true iff |aChar| matches [a-zA-Z].
  30. *
  31. * This function is basically what you thought isalpha was, except its behavior
  32. * doesn't depend on the user's current locale.
  33. */
  34. template<typename Char>
  35. constexpr bool
  36. IsAsciiAlpha(Char aChar)
  37. {
  38. using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
  39. return ('a' <= static_cast<UnsignedChar>(aChar) &&
  40. static_cast<UnsignedChar>(aChar) <= 'z') ||
  41. ('A' <= static_cast<UnsignedChar>(aChar) &&
  42. static_cast<UnsignedChar>(aChar) <= 'Z');
  43. }
  44. } // namespace mozilla
  45. #endif /* mozilla_TextUtils_h */