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

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

Issue 2764283002: Move files in wtf/ to platform/wtf/ (Part 10). (Closed)
Patch Set: Rebase. Created 3 years, 9 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 // Copyright 2017 The Chromium Authors. All rights reserved.
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be
3 * 3 // found in the LICENSE file.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 4
31 #ifndef StringUTF8Adaptor_h 5 #include "platform/wtf/text/StringUTF8Adaptor.h"
32 #define StringUTF8Adaptor_h
33 6
34 #include "base/strings/string_piece.h" 7 // The contents of this header was moved to platform/wtf as part of
35 #include "wtf/Allocator.h" 8 // WTF migration project. See the following post for details:
36 #include "wtf/text/CString.h" 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY CAAJ
37 #include "wtf/text/TextEncoding.h"
38 #include "wtf/text/WTFString.h"
39
40 namespace WTF {
41
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
44 // contain only ASCII characters.
45 class StringUTF8Adaptor final {
46 DISALLOW_NEW();
47
48 public:
49 StringUTF8Adaptor(const String& string,
50 UTF8ConversionMode mode = LenientUTF8Conversion)
51 : m_data(0), m_length(0) {
52 if (string.isEmpty())
53 return;
54 // Unfortunately, 8 bit WTFStrings are encoded in Latin-1 and GURL uses
55 // UTF-8 when processing 8 bit strings. If |relative| is entirely ASCII, we
56 // luck out and can avoid mallocing a new buffer to hold the UTF-8 data
57 // because UTF-8 and Latin-1 use the same code units for ASCII code points.
58 if (string.is8Bit() && string.containsOnlyASCII()) {
59 m_data = reinterpret_cast<const char*>(string.characters8());
60 m_length = string.length();
61 } else {
62 m_utf8Buffer = string.utf8(mode);
63 m_data = m_utf8Buffer.data();
64 m_length = m_utf8Buffer.length();
65 }
66 }
67
68 const char* data() const { return m_data; }
69 size_t length() const { return m_length; }
70
71 base::StringPiece asStringPiece() const {
72 return base::StringPiece(m_data, m_length);
73 }
74
75 private:
76 CString m_utf8Buffer;
77 const char* m_data;
78 size_t m_length;
79 };
80
81 } // namespace WTF
82
83 using WTF::StringUTF8Adaptor;
84
85 #endif // StringUTF8Adaptor_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/ParsingUtilities.h ('k') | third_party/WebKit/Source/wtf/text/TextCodec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698