OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com> | 3 // found in the LICENSE file. |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 */ | |
26 | 4 |
27 #ifndef TextCodec_h | 5 #include "platform/wtf/text/TextCodec.h" |
28 #define TextCodec_h | |
29 | 6 |
30 #include "wtf/Forward.h" | 7 // The contents of this header was moved to platform/wtf as part of |
31 #include "wtf/Noncopyable.h" | 8 // WTF migration project. See the following post for details: |
32 #include "wtf/text/Unicode.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
33 #include "wtf/text/WTFString.h" | |
34 #include <memory> | |
35 | |
36 namespace WTF { | |
37 | |
38 class TextEncoding; | |
39 | |
40 // Specifies what will happen when a character is encountered that is | |
41 // not encodable in the character set. | |
42 enum UnencodableHandling { | |
43 // Substitutes the replacement character "?". | |
44 QuestionMarksForUnencodables, | |
45 | |
46 // Encodes the character as an XML entity. For example, U+06DE | |
47 // would be "۞" (0x6DE = 1758 in octal). | |
48 EntitiesForUnencodables, | |
49 | |
50 // Encodes the character as en entity as above, but escaped | |
51 // non-alphanumeric characters. This is used in URLs. | |
52 // For example, U+6DE would be "%26%231758%3B". | |
53 URLEncodedEntitiesForUnencodables, | |
54 | |
55 // Encodes the character as a CSS entity. For example U+06DE | |
56 // would be \06de. See: https://www.w3.org/TR/css-syntax-3/#escaping | |
57 CSSEncodedEntitiesForUnencodables, | |
58 }; | |
59 | |
60 typedef char UnencodableReplacementArray[32]; | |
61 | |
62 enum FlushBehavior { | |
63 // More bytes are coming, don't flush the codec. | |
64 DoNotFlush = 0, | |
65 | |
66 // A fetch has hit EOF. Some codecs handle fetches differently, for compat | |
67 // reasons. | |
68 FetchEOF, | |
69 | |
70 // Do a full flush of the codec. | |
71 DataEOF | |
72 }; | |
73 | |
74 static_assert(!DoNotFlush, "DoNotFlush should be falsy"); | |
75 static_assert(FetchEOF, "FetchEOF should be truthy"); | |
76 static_assert(DataEOF, "DataEOF should be truthy"); | |
77 | |
78 class WTF_EXPORT TextCodec { | |
79 WTF_MAKE_NONCOPYABLE(TextCodec); | |
80 USING_FAST_MALLOC(TextCodec); | |
81 | |
82 public: | |
83 TextCodec() {} | |
84 virtual ~TextCodec(); | |
85 | |
86 String decode(const char* str, | |
87 size_t length, | |
88 FlushBehavior flush = DoNotFlush) { | |
89 bool ignored; | |
90 return decode(str, length, flush, false, ignored); | |
91 } | |
92 | |
93 virtual String decode(const char*, | |
94 size_t length, | |
95 FlushBehavior, | |
96 bool stopOnError, | |
97 bool& sawError) = 0; | |
98 virtual CString encode(const UChar*, size_t length, UnencodableHandling) = 0; | |
99 virtual CString encode(const LChar*, size_t length, UnencodableHandling) = 0; | |
100 | |
101 // Fills a null-terminated string representation of the given | |
102 // unencodable character into the given replacement buffer. | |
103 // The length of the string (not including the null) will be returned. | |
104 static int getUnencodableReplacement(unsigned codePoint, | |
105 UnencodableHandling, | |
106 UnencodableReplacementArray); | |
107 }; | |
108 | |
109 typedef void (*EncodingNameRegistrar)(const char* alias, const char* name); | |
110 | |
111 typedef std::unique_ptr<TextCodec> ( | |
112 *NewTextCodecFunction)(const TextEncoding&, const void* additionalData); | |
113 typedef void (*TextCodecRegistrar)(const char* name, | |
114 NewTextCodecFunction, | |
115 const void* additionalData); | |
116 | |
117 } // namespace WTF | |
118 | |
119 using WTF::TextCodec; | |
120 | |
121 #endif // TextCodec_h | |
OLD | NEW |