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