source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/utils/converters/utf8/src/utf8.h@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 2.1 KB
Line 
1// Copyright 2011 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you
4// may not use this file except in compliance with the License. You
5// may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied. See the License for the specific language governing
13// permissions and limitations under the License.
14
15#ifndef WEBGL_LOADER_UTF8_H_
16#define WEBGL_LOADER_UTF8_H_
17
18#include "base.h"
19#include "stream.h"
20
21namespace webgl_loader {
22
23const uint8 kUtf8MoreBytesPrefix = 0x80;
24const uint8 kUtf8TwoBytePrefix = 0xC0;
25const uint8 kUtf8ThreeBytePrefix = 0xE0;
26
27const uint16 kUtf8TwoByteLimit = 0x0800;
28const uint16 kUtf8SurrogatePairStart = 0xD800;
29const uint16 kUtf8SurrogatePairNum = 0x0800;
30const uint16 kUtf8EncodableEnd = 0x10000 - kUtf8SurrogatePairNum;
31
32const uint16 kUtf8MoreBytesMask = 0x3F;
33
34bool Uint16ToUtf8(uint16 word, ByteSinkInterface* sink) {
35 if (word < 0x80) {
36 sink->Put(static_cast<char>(word));
37 } else if (word < kUtf8TwoByteLimit) {
38 sink->Put(static_cast<char>(kUtf8TwoBytePrefix + (word >> 6)));
39 sink->Put(static_cast<char>(kUtf8MoreBytesPrefix +
40 (word & kUtf8MoreBytesMask)));
41 } else if (word < kUtf8EncodableEnd) {
42 // We can only encode 65535 - 2048 values because of illegal UTF-8
43 // characters, such as surrogate pairs in [0xD800, 0xDFFF].
44 if (word >= kUtf8SurrogatePairStart) {
45 // Shift the result to avoid the surrogate pair range.
46 word += kUtf8SurrogatePairNum;
47 }
48 sink->Put(static_cast<char>(kUtf8ThreeBytePrefix + (word >> 12)));
49 sink->Put(static_cast<char>(kUtf8MoreBytesPrefix +
50 ((word >> 6) & kUtf8MoreBytesMask)));
51 sink->Put(static_cast<char>(kUtf8MoreBytesPrefix +
52 (word & kUtf8MoreBytesMask)));
53 } else {
54 return false;
55 }
56 return true;
57}
58
59} // namespace webgl_loader
60
61#endif // WEBGL_LOADER_UTF8_H_
Note: See TracBrowser for help on using the repository browser.