Debian Codegraph @

mfbt/Utf8.cpp

raw
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/. */
6
7 #define MOZ_PRETEND_NO_JSRUST 1
8
9 #include "mozilla/Maybe.h"
10 #include "mozilla/TextUtils.h"
11 #include "mozilla/Types.h"
12 #include "mozilla/Utf8.h"
13
14 #include <stddef.h>
15 #include <stdint.h>
16
17 MFBT_API bool mozilla::detail::IsValidUtf8(const void* aCodeUnits,
18 size_t aCount) {
19 const auto* s = reinterpret_cast<const unsigned char*>(aCodeUnits);
20 const auto* const limit = s + aCount;
21
22 while (s < limit) {
23 unsigned char c = *s++;
24
25 // If the first byte is ASCII, it's the only one in the code point. Have a
26 // fast path that avoids all the rest of the work and looping in that case.
27 if (IsAscii(c)) {
28 continue;
29 }
30
31 Maybe<char32_t> maybeCodePoint =
32 DecodeOneUtf8CodePoint(Utf8Unit(c), &s, limit);
33 if (maybeCodePoint.isNothing()) {
34 return false;
35 }
36 }
37
38 MOZ_ASSERT(s == limit);
39 return true;
40 }