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