summaryrefslogtreecommitdiff
path: root/mfbt/TextUtils.h
diff options
context:
space:
mode:
authorJeff Walden <jwalden@mit.edu>2018-04-09 12:02:43 -0700
committerwolfbeast <mcwerewolf@gmail.com>2018-04-29 13:39:27 +0200
commitd308db119d48ce858b194a720f7946349b5f9b47 (patch)
tree2e0ad36f4fec1a5a79300b4ed449ad28e7e9ae64 /mfbt/TextUtils.h
parent66aa6b595c47231958a855e4b166f5d55df1184d (diff)
downloaduxp-d308db119d48ce858b194a720f7946349b5f9b47.tar.gz
Bug 1452619 - Implement mozilla::IsAsciiAlpha. r=froydnj, a=lizzard
Diffstat (limited to 'mfbt/TextUtils.h')
-rw-r--r--mfbt/TextUtils.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/mfbt/TextUtils.h b/mfbt/TextUtils.h
new file mode 100644
index 0000000000..9494296ce1
--- /dev/null
+++ b/mfbt/TextUtils.h
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* Character/text operations. */
+
+#ifndef mozilla_TextUtils_h
+#define mozilla_TextUtils_h
+
+#include "mozilla/TypeTraits.h"
+
+namespace mozilla {
+
+namespace detail {
+
+template<typename Char>
+class MakeUnsignedChar
+ : public MakeUnsigned<Char>
+{};
+
+template<>
+class MakeUnsignedChar<char16_t>
+{
+public:
+ using Type = char16_t;
+};
+
+template<>
+class MakeUnsignedChar<char32_t>
+{
+public:
+ using Type = char32_t;
+};
+
+} // namespace detail
+
+/**
+ * Returns true iff |aChar| matches [a-zA-Z].
+ *
+ * This function is basically what you thought isalpha was, except its behavior
+ * doesn't depend on the user's current locale.
+ */
+template<typename Char>
+constexpr bool
+IsAsciiAlpha(Char aChar)
+{
+ using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
+ return ('a' <= static_cast<UnsignedChar>(aChar) &&
+ static_cast<UnsignedChar>(aChar) <= 'z') ||
+ ('A' <= static_cast<UnsignedChar>(aChar) &&
+ static_cast<UnsignedChar>(aChar) <= 'Z');
+}
+
+} // namespace mozilla
+
+#endif /* mozilla_TextUtils_h */