OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google 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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // This class lets you get UTF-8 data out of a String without mallocing a | 42 // This class lets you get UTF-8 data out of a String without mallocing a |
43 // separate buffer to hold the data if the String happens to be 8 bit and | 43 // separate buffer to hold the data if the String happens to be 8 bit and |
44 // contain only ASCII characters. | 44 // contain only ASCII characters. |
45 class StringUTF8Adaptor final { | 45 class StringUTF8Adaptor final { |
46 DISALLOW_NEW(); | 46 DISALLOW_NEW(); |
47 | 47 |
48 public: | 48 public: |
49 explicit StringUTF8Adaptor(const String& string) : m_data(0), m_length(0) { | 49 explicit StringUTF8Adaptor(const String& string) : m_data(0), m_length(0) { |
50 if (string.isEmpty()) | 50 if (string.isEmpty()) |
51 return; | 51 return; |
52 // Unfortunately, 8 bit WTFStrings are encoded in Latin-1 and GURL uses UTF-
8 | 52 // Unfortunately, 8 bit WTFStrings are encoded in Latin-1 and GURL uses |
53 // when processing 8 bit strings. If |relative| is entirely ASCII, we luck o
ut | 53 // UTF-8 when processing 8 bit strings. If |relative| is entirely ASCII, we |
54 // and can avoid mallocing a new buffer to hold the UTF-8 data because UTF-8 | 54 // luck out and can avoid mallocing a new buffer to hold the UTF-8 data |
55 // and Latin-1 use the same code units for ASCII code points. | 55 // because UTF-8 and Latin-1 use the same code units for ASCII code points. |
56 if (string.is8Bit() && string.containsOnlyASCII()) { | 56 if (string.is8Bit() && string.containsOnlyASCII()) { |
57 m_data = reinterpret_cast<const char*>(string.characters8()); | 57 m_data = reinterpret_cast<const char*>(string.characters8()); |
58 m_length = string.length(); | 58 m_length = string.length(); |
59 } else { | 59 } else { |
60 m_utf8Buffer = string.utf8(); | 60 m_utf8Buffer = string.utf8(); |
61 m_data = m_utf8Buffer.data(); | 61 m_data = m_utf8Buffer.data(); |
62 m_length = m_utf8Buffer.length(); | 62 m_length = m_utf8Buffer.length(); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 const char* data() const { return m_data; } | 66 const char* data() const { return m_data; } |
67 size_t length() const { return m_length; } | 67 size_t length() const { return m_length; } |
68 | 68 |
69 base::StringPiece asStringPiece() const { | 69 base::StringPiece asStringPiece() const { |
70 return base::StringPiece(m_data, m_length); | 70 return base::StringPiece(m_data, m_length); |
71 } | 71 } |
72 | 72 |
73 private: | 73 private: |
74 CString m_utf8Buffer; | 74 CString m_utf8Buffer; |
75 const char* m_data; | 75 const char* m_data; |
76 size_t m_length; | 76 size_t m_length; |
77 }; | 77 }; |
78 | 78 |
79 } // namespace WTF | 79 } // namespace WTF |
80 | 80 |
81 using WTF::StringUTF8Adaptor; | 81 using WTF::StringUTF8Adaptor; |
82 | 82 |
83 #endif // StringUTF8Adaptor_h | 83 #endif // StringUTF8Adaptor_h |
OLD | NEW |