Bug 1887677: Correctly compute inClass predicate when validating unbound private...
[gecko.git] / mfbt / Utf8.cpp
blob1a10c7a011685083848076a019fd737330f4bf4b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Maybe.h"
8 #include "mozilla/TextUtils.h"
9 #include "mozilla/Types.h"
10 #include "mozilla/Utf8.h"
12 #include <stddef.h>
13 #include <stdint.h>
15 MFBT_API bool mozilla::detail::IsValidUtf8(const void* aCodeUnits,
16 size_t aCount) {
17 const auto* s = reinterpret_cast<const unsigned char*>(aCodeUnits);
18 const auto* const limit = s + aCount;
20 while (s < limit) {
21 unsigned char c = *s++;
23 // If the first byte is ASCII, it's the only one in the code point. Have a
24 // fast path that avoids all the rest of the work and looping in that case.
25 if (IsAscii(c)) {
26 continue;
29 Maybe<char32_t> maybeCodePoint =
30 DecodeOneUtf8CodePoint(Utf8Unit(c), &s, limit);
31 if (maybeCodePoint.isNothing()) {
32 return false;
36 MOZ_ASSERT(s == limit);
37 return true;