| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "wtf/text/TextCodecReplacement.h" | 5 #include "wtf/text/TextCodecReplacement.h" |
| 6 | 6 |
| 7 #include "wtf/PtrUtil.h" | 7 #include "wtf/PassOwnPtr.h" |
| 8 #include "wtf/text/CharacterNames.h" | 8 #include "wtf/text/CharacterNames.h" |
| 9 #include "wtf/text/WTFString.h" | 9 #include "wtf/text/WTFString.h" |
| 10 #include <memory> | |
| 11 | 10 |
| 12 namespace WTF { | 11 namespace WTF { |
| 13 | 12 |
| 14 TextCodecReplacement::TextCodecReplacement() | 13 TextCodecReplacement::TextCodecReplacement() |
| 15 : m_replacementErrorReturned(false) | 14 : m_replacementErrorReturned(false) |
| 16 { | 15 { |
| 17 } | 16 } |
| 18 | 17 |
| 19 void TextCodecReplacement::registerEncodingNames(EncodingNameRegistrar registrar
) | 18 void TextCodecReplacement::registerEncodingNames(EncodingNameRegistrar registrar
) |
| 20 { | 19 { |
| 21 // The 'replacement' label itself should not be referenceable by | 20 // The 'replacement' label itself should not be referenceable by |
| 22 // resources or script - it's a specification convenience - but much of | 21 // resources or script - it's a specification convenience - but much of |
| 23 // the wtf/text API asserts that an encoding name is a label for itself. | 22 // the wtf/text API asserts that an encoding name is a label for itself. |
| 24 // This is handled in TextEncoding by marking it as not valid. | 23 // This is handled in TextEncoding by marking it as not valid. |
| 25 registrar("replacement", "replacement"); | 24 registrar("replacement", "replacement"); |
| 26 | 25 |
| 27 registrar("csiso2022kr", "replacement"); | 26 registrar("csiso2022kr", "replacement"); |
| 28 registrar("hz-gb-2312", "replacement"); | 27 registrar("hz-gb-2312", "replacement"); |
| 29 registrar("iso-2022-cn", "replacement"); | 28 registrar("iso-2022-cn", "replacement"); |
| 30 registrar("iso-2022-cn-ext", "replacement"); | 29 registrar("iso-2022-cn-ext", "replacement"); |
| 31 registrar("iso-2022-kr", "replacement"); | 30 registrar("iso-2022-kr", "replacement"); |
| 32 } | 31 } |
| 33 | 32 |
| 34 static std::unique_ptr<TextCodec> newStreamingTextDecoderReplacement(const TextE
ncoding&, const void*) | 33 static PassOwnPtr<TextCodec> newStreamingTextDecoderReplacement(const TextEncodi
ng&, const void*) |
| 35 { | 34 { |
| 36 return wrapUnique(new TextCodecReplacement); | 35 return adoptPtr(new TextCodecReplacement); |
| 37 } | 36 } |
| 38 | 37 |
| 39 void TextCodecReplacement::registerCodecs(TextCodecRegistrar registrar) | 38 void TextCodecReplacement::registerCodecs(TextCodecRegistrar registrar) |
| 40 { | 39 { |
| 41 registrar("replacement", newStreamingTextDecoderReplacement, 0); | 40 registrar("replacement", newStreamingTextDecoderReplacement, 0); |
| 42 } | 41 } |
| 43 | 42 |
| 44 String TextCodecReplacement::decode(const char*, size_t length, FlushBehavior, b
ool, bool& sawError) | 43 String TextCodecReplacement::decode(const char*, size_t length, FlushBehavior, b
ool, bool& sawError) |
| 45 { | 44 { |
| 46 // https://encoding.spec.whatwg.org/#replacement-decoder | 45 // https://encoding.spec.whatwg.org/#replacement-decoder |
| 47 | 46 |
| 48 // 1. If byte is end-of-stream, return finished. | 47 // 1. If byte is end-of-stream, return finished. |
| 49 if (!length) | 48 if (!length) |
| 50 return String(); | 49 return String(); |
| 51 | 50 |
| 52 // 2. If replacement error returned flag is unset, set the replacement | 51 // 2. If replacement error returned flag is unset, set the replacement |
| 53 // error returned flag and return error. | 52 // error returned flag and return error. |
| 54 if (!m_replacementErrorReturned) { | 53 if (!m_replacementErrorReturned) { |
| 55 m_replacementErrorReturned = true; | 54 m_replacementErrorReturned = true; |
| 56 sawError = true; | 55 sawError = true; |
| 57 return String(&replacementCharacter, 1); | 56 return String(&replacementCharacter, 1); |
| 58 } | 57 } |
| 59 | 58 |
| 60 // 3. Return finished. | 59 // 3. Return finished. |
| 61 return String(); | 60 return String(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 } // namespace WTF | 63 } // namespace WTF |
| OLD | NEW |