OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Copyright 2010 Google Inc. |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 |
| 17 // Author: Shawn Ligocki |
| 18 |
| 19 #include "utf/unilib.h" |
| 20 |
| 21 #include "base/basictypes.h" |
| 22 #include "utf/utf.h" |
| 23 |
| 24 namespace UniLib { |
| 25 |
| 26 namespace { |
| 27 |
| 28 // MOE: start_strip |
| 29 // MOE: end_strip |
| 30 // Codepoints not allowed for interchange are: |
| 31 // C0 (ASCII) controls: U+0000 to U+001F excluding Space (SP, U+0020), |
| 32 // Horizontal Tab (HT, U+0009), Line-Feed (LF, U+000A), |
| 33 // Form Feed (FF, U+000C) and Carriage-Return (CR, U+000D) |
| 34 // C1 controls: U+007F to U+009F |
| 35 // Surrogates: U+D800 to U+DFFF |
| 36 // Non-characters: U+FDD0 to U+FDEF and U+xxFFFE to U+xxFFFF for all xx |
| 37 inline bool IsInterchangeValidCodepoint(char32 c) { |
| 38 return !((c >= 0x00 && c <= 0x08) || c == 0x0B || (c >= 0x0E && c <= 0x1F) || |
| 39 (c >= 0x7F && c <= 0x9F) || |
| 40 (c >= 0xD800 && c <= 0xDFFF) || |
| 41 (c >= 0xFDD0 && c <= 0xFDEF) || (c&0xFFFE) == 0xFFFE); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 int SpanInterchangeValid(const char* begin, int byte_length) { |
| 47 char32 rune; |
| 48 const char* p = begin; |
| 49 const char* end = begin + byte_length; |
| 50 while (p < end) { |
| 51 int bytes_consumed = charntorune(&rune, p, end - p); |
| 52 // We want to accept Runeerror == U+FFFD as a valid char, but it is used |
| 53 // by chartorune to indicate error. Luckily, the real codepoint is size 3 |
| 54 // while errors return bytes_consumed == 1. |
| 55 if ((rune == Runeerror && bytes_consumed == 1) || |
| 56 !IsInterchangeValidCodepoint(rune)) { |
| 57 break; // Found |
| 58 } |
| 59 p += bytes_consumed; |
| 60 } |
| 61 return p - begin; |
| 62 } |
| 63 |
| 64 } // namespace UniLib |
OLD | NEW |