1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A streaming validator for UTF-8. Validation is based on the definition in
6 // RFC-3629. In particular, it does not reject the invalid characters rejected
7 // by base::IsStringUTF8().
9 // The implementation detects errors on the first possible byte.
11 #ifndef BASE_I18N_STREAMING_UTF8_VALIDATOR_H_
12 #define BASE_I18N_STREAMING_UTF8_VALIDATOR_H_
19 #include "base/i18n/base_i18n_export.h"
20 #include "base/macros.h"
24 class BASE_I18N_EXPORT StreamingUtf8Validator {
26 // The validator exposes 3 states. It starts in state VALID_ENDPOINT. As it
27 // processes characters it alternates between VALID_ENDPOINT and
28 // VALID_MIDPOINT. If it encounters an invalid byte or UTF-8 sequence the
29 // state changes permanently to INVALID.
36 StreamingUtf8Validator() : state_(0u) {}
37 // Trivial destructor intentionally omitted.
39 // Validate |size| bytes starting at |data|. If the concatenation of all calls
40 // to AddBytes() since this object was constructed or reset is a valid UTF-8
41 // string, returns VALID_ENDPOINT. If it could be the prefix of a valid UTF-8
42 // string, returns VALID_MIDPOINT. If an invalid byte or UTF-8 sequence was
43 // present, returns INVALID.
44 State AddBytes(const char* data, size_t size);
46 // Return the object to a freshly-constructed state so that it can be re-used.
49 // Validate a complete string using the same criteria. Returns true if the
50 // string only contains complete, valid UTF-8 codepoints.
51 static bool Validate(const std::string& string);
54 // The current state of the validator. Value 0 is the initial/valid state.
55 // The state is stored as an offset into |kUtf8ValidatorTables|. The special
56 // state |kUtf8InvalidState| is invalid.
59 // This type could be made copyable but there is currently no use-case for
61 DISALLOW_COPY_AND_ASSIGN(StreamingUtf8Validator);
66 #endif // BASE_I18N_STREAMING_UTF8_VALIDATOR_H_