Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h

Issue 2629573002: Allow WebString.utf8() to take UTF8ConversionMode (Closed)
Patch Set: comment fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 28 matching lines...) Expand all
39 39
40 namespace WTF { 40 namespace WTF {
41 41
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 StringUTF8Adaptor(const String& string,
50 UTF8ConversionMode mode = LenientUTF8Conversion)
51 : m_data(0), m_length(0) {
50 if (string.isEmpty()) 52 if (string.isEmpty())
51 return; 53 return;
52 // Unfortunately, 8 bit WTFStrings are encoded in Latin-1 and GURL uses 54 // Unfortunately, 8 bit WTFStrings are encoded in Latin-1 and GURL uses
53 // UTF-8 when processing 8 bit strings. If |relative| is entirely ASCII, we 55 // UTF-8 when processing 8 bit strings. If |relative| is entirely ASCII, we
54 // luck out and can avoid mallocing a new buffer to hold the UTF-8 data 56 // luck out and can avoid mallocing a new buffer to hold the UTF-8 data
55 // because UTF-8 and Latin-1 use the same code units for ASCII code points. 57 // because UTF-8 and Latin-1 use the same code units for ASCII code points.
56 if (string.is8Bit() && string.containsOnlyASCII()) { 58 if (string.is8Bit() && string.containsOnlyASCII()) {
57 m_data = reinterpret_cast<const char*>(string.characters8()); 59 m_data = reinterpret_cast<const char*>(string.characters8());
58 m_length = string.length(); 60 m_length = string.length();
59 } else { 61 } else {
60 m_utf8Buffer = string.utf8(); 62 m_utf8Buffer = string.utf8(mode);
61 m_data = m_utf8Buffer.data(); 63 m_data = m_utf8Buffer.data();
62 m_length = m_utf8Buffer.length(); 64 m_length = m_utf8Buffer.length();
63 } 65 }
64 } 66 }
65 67
66 const char* data() const { return m_data; } 68 const char* data() const { return m_data; }
67 size_t length() const { return m_length; } 69 size_t length() const { return m_length; }
68 70
69 base::StringPiece asStringPiece() const { 71 base::StringPiece asStringPiece() const {
70 return base::StringPiece(m_data, m_length); 72 return base::StringPiece(m_data, m_length);
71 } 73 }
72 74
73 private: 75 private:
74 CString m_utf8Buffer; 76 CString m_utf8Buffer;
75 const char* m_data; 77 const char* m_data;
76 size_t m_length; 78 size_t m_length;
77 }; 79 };
78 80
79 } // namespace WTF 81 } // namespace WTF
80 82
81 using WTF::StringUTF8Adaptor; 83 using WTF::StringUTF8Adaptor;
82 84
83 #endif // StringUTF8Adaptor_h 85 #endif // StringUTF8Adaptor_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/exported/WebStringTest.cpp ('k') | third_party/WebKit/public/platform/WebString.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698