| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008 Apple, Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008 Apple, Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "wtf/text/TextCodecUserDefined.h" | 26 #include "wtf/text/TextCodecUserDefined.h" |
| 27 | 27 |
| 28 #include "wtf/PassOwnPtr.h" | 28 #include "wtf/PassOwnPtr.h" |
| 29 #include "wtf/text/CString.h" | 29 #include "wtf/text/CString.h" |
| 30 #include "wtf/text/StringBuffer.h" | 30 #include "wtf/text/StringBuffer.h" |
| 31 #include "wtf/text/StringBuilder.h" | 31 #include "wtf/text/StringBuilder.h" |
| 32 #include "wtf/text/WTFString.h" | 32 #include "wtf/text/WTFString.h" |
| 33 | 33 |
| 34 namespace WTF { | 34 namespace WTF { |
| 35 | 35 |
| 36 void TextCodecUserDefined::registerEncodingNames(EncodingNameRegistrar registrar
) | 36 void TextCodecUserDefined::registerEncodingNames( |
| 37 { | 37 EncodingNameRegistrar registrar) { |
| 38 registrar("x-user-defined", "x-user-defined"); | 38 registrar("x-user-defined", "x-user-defined"); |
| 39 } | 39 } |
| 40 | 40 |
| 41 static PassOwnPtr<TextCodec> newStreamingTextDecoderUserDefined(const TextEncodi
ng&, const void*) | 41 static PassOwnPtr<TextCodec> newStreamingTextDecoderUserDefined( |
| 42 { | 42 const TextEncoding&, |
| 43 return adoptPtr(new TextCodecUserDefined); | 43 const void*) { |
| 44 return adoptPtr(new TextCodecUserDefined); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void TextCodecUserDefined::registerCodecs(TextCodecRegistrar registrar) | 47 void TextCodecUserDefined::registerCodecs(TextCodecRegistrar registrar) { |
| 47 { | 48 registrar("x-user-defined", newStreamingTextDecoderUserDefined, 0); |
| 48 registrar("x-user-defined", newStreamingTextDecoderUserDefined, 0); | |
| 49 } | 49 } |
| 50 | 50 |
| 51 String TextCodecUserDefined::decode(const char* bytes, size_t length, FlushBehav
ior, bool, bool&) | 51 String TextCodecUserDefined::decode(const char* bytes, |
| 52 { | 52 size_t length, |
| 53 StringBuilder result; | 53 FlushBehavior, |
| 54 result.reserveCapacity(length); | 54 bool, |
| 55 bool&) { |
| 56 StringBuilder result; |
| 57 result.reserveCapacity(length); |
| 55 | 58 |
| 56 for (size_t i = 0; i < length; ++i) { | 59 for (size_t i = 0; i < length; ++i) { |
| 57 signed char c = bytes[i]; | 60 signed char c = bytes[i]; |
| 58 result.append(static_cast<UChar>(c & 0xF7FF)); | 61 result.append(static_cast<UChar>(c & 0xF7FF)); |
| 59 } | 62 } |
| 60 | 63 |
| 61 return result.toString(); | 64 return result.toString(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 template<typename CharType> | 67 template <typename CharType> |
| 65 static CString encodeComplexUserDefined(const CharType* characters, size_t lengt
h, UnencodableHandling handling) | 68 static CString encodeComplexUserDefined(const CharType* characters, |
| 66 { | 69 size_t length, |
| 67 Vector<char> result(length); | 70 UnencodableHandling handling) { |
| 68 char* bytes = result.data(); | 71 Vector<char> result(length); |
| 72 char* bytes = result.data(); |
| 69 | 73 |
| 70 size_t resultLength = 0; | 74 size_t resultLength = 0; |
| 71 for (size_t i = 0; i < length; ) { | 75 for (size_t i = 0; i < length;) { |
| 72 UChar32 c; | 76 UChar32 c; |
| 73 U16_NEXT(characters, i, length, c); | 77 U16_NEXT(characters, i, length, c); |
| 74 signed char signedByte = static_cast<signed char>(c); | 78 signed char signedByte = static_cast<signed char>(c); |
| 75 if ((signedByte & 0xF7FF) == c) { | 79 if ((signedByte & 0xF7FF) == c) { |
| 76 bytes[resultLength++] = signedByte; | 80 bytes[resultLength++] = signedByte; |
| 77 } else { | 81 } else { |
| 78 // No way to encode this character with x-user-defined. | 82 // No way to encode this character with x-user-defined. |
| 79 UnencodableReplacementArray replacement; | 83 UnencodableReplacementArray replacement; |
| 80 int replacementLength = TextCodec::getUnencodableReplacement(c, hand
ling, replacement); | 84 int replacementLength = |
| 81 result.grow(resultLength + replacementLength + length - i); | 85 TextCodec::getUnencodableReplacement(c, handling, replacement); |
| 82 bytes = result.data(); | 86 result.grow(resultLength + replacementLength + length - i); |
| 83 memcpy(bytes + resultLength, replacement, replacementLength); | 87 bytes = result.data(); |
| 84 resultLength += replacementLength; | 88 memcpy(bytes + resultLength, replacement, replacementLength); |
| 85 } | 89 resultLength += replacementLength; |
| 86 } | 90 } |
| 91 } |
| 87 | 92 |
| 88 return CString(bytes, resultLength); | 93 return CString(bytes, resultLength); |
| 89 } | 94 } |
| 90 | 95 |
| 91 template<typename CharType> | 96 template <typename CharType> |
| 92 CString TextCodecUserDefined::encodeCommon(const CharType* characters, size_t le
ngth, UnencodableHandling handling) | 97 CString TextCodecUserDefined::encodeCommon(const CharType* characters, |
| 93 { | 98 size_t length, |
| 94 char* bytes; | 99 UnencodableHandling handling) { |
| 95 CString result = CString::newUninitialized(length, bytes); | 100 char* bytes; |
| 101 CString result = CString::newUninitialized(length, bytes); |
| 96 | 102 |
| 97 // Convert the string a fast way and simultaneously do an efficient check to
see if it's all ASCII. | 103 // Convert the string a fast way and simultaneously do an efficient check to s
ee if it's all ASCII. |
| 98 UChar ored = 0; | 104 UChar ored = 0; |
| 99 for (size_t i = 0; i < length; ++i) { | 105 for (size_t i = 0; i < length; ++i) { |
| 100 UChar c = characters[i]; | 106 UChar c = characters[i]; |
| 101 bytes[i] = static_cast<char>(c); | 107 bytes[i] = static_cast<char>(c); |
| 102 ored |= c; | 108 ored |= c; |
| 103 } | 109 } |
| 104 | 110 |
| 105 if (!(ored & 0xFF80)) | 111 if (!(ored & 0xFF80)) |
| 106 return result; | 112 return result; |
| 107 | 113 |
| 108 // If it wasn't all ASCII, call the function that handles more-complex cases
. | 114 // If it wasn't all ASCII, call the function that handles more-complex cases. |
| 109 return encodeComplexUserDefined(characters, length, handling); | 115 return encodeComplexUserDefined(characters, length, handling); |
| 110 } | 116 } |
| 111 | 117 |
| 112 CString TextCodecUserDefined::encode(const UChar* characters, size_t length, Une
ncodableHandling handling) | 118 CString TextCodecUserDefined::encode(const UChar* characters, |
| 113 { | 119 size_t length, |
| 114 return encodeCommon(characters, length, handling); | 120 UnencodableHandling handling) { |
| 121 return encodeCommon(characters, length, handling); |
| 115 } | 122 } |
| 116 | 123 |
| 117 CString TextCodecUserDefined::encode(const LChar* characters, size_t length, Une
ncodableHandling handling) | 124 CString TextCodecUserDefined::encode(const LChar* characters, |
| 118 { | 125 size_t length, |
| 119 return encodeCommon(characters, length, handling); | 126 UnencodableHandling handling) { |
| 127 return encodeCommon(characters, length, handling); |
| 120 } | 128 } |
| 121 | 129 |
| 122 } // namespace WTF | 130 } // namespace WTF |
| OLD | NEW |