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

Side by Side Diff: base/wide_string_piece.h

Issue 7549003: Optimize phishing page term feature extraction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove unnecesary macros in wide_string_piece_unittest.cc Created 9 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // WideStringPiece is similar to StringPiece (in base/string_piece.h) but for
6 // base::string16 instead of std::string. We do not define as large of a subset
7 // of the STL functions from basic_string as in StringPiece, but this can be
8 // changed if these functions (find, find_first_of, etc.) are found to be
9 // useful in this context.
10 //
11
12 #ifndef BASE_WIDE_STRING_PIECE_H_
13 #define BASE_WIDE_STRING_PIECE_H_
14 #pragma once
15
16 #include "base/base_api.h"
17 #include "base/string16.h"
18
19 namespace base {
20
21 class BASE_API WideStringPiece {
22 public:
23 // standard STL container boilerplate
24 typedef size_t size_type;
25 typedef char16 value_type;
26 typedef const char16* pointer;
27 typedef const char16& reference;
28 typedef const char16& const_reference;
29 typedef ptrdiff_t difference_type;
30 typedef const char16* const_iterator;
31 typedef const char16* iterator;
32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
33 typedef std::reverse_iterator<iterator> reverse_iterator;
34
35 public:
36 // We provide non-explicit singleton constructors so users can pass
37 // in a "const char16*" or a "string16" wherever a "WideStringPiece" is
38 // expected.
39 WideStringPiece() : ptr_(NULL), length_(0) { }
40 WideStringPiece(const char16* str)
41 : ptr_(str), length_((str == NULL) ? 0 : c16len(str)) { }
42 WideStringPiece(const string16& str)
43 : ptr_(str.data()), length_(str.size()) { }
44 WideStringPiece(const char16* offset, size_type len)
45 : ptr_(offset), length_(len) { }
46
47 // data() may return a pointer to a buffer with embedded NULs, and the
48 // returned buffer may or may not be null terminated. Therefore it is
49 // typically a mistake to pass data() to a routine that expects a NUL
50 // terminated string.
51 const char16* data() const { return ptr_; }
52 size_type size() const { return length_; }
53 size_type length() const { return length_; }
54 bool empty() const { return length_ == 0; }
55
56 void clear() {
57 ptr_ = NULL;
58 length_ = 0;
59 }
60 void set(const char16* data, size_type len) {
61 ptr_ = data;
62 length_ = len;
63 }
64 void set(const char16* str) {
65 ptr_ = str;
66 length_ = str ? c16len(str) : 0;
67 }
68 void set(const void* data, size_type len) {
69 ptr_ = reinterpret_cast<const char16*>(data);
70 length_ = len;
71 }
72
73 char16 operator[](size_type i) const { return ptr_[i]; }
74
75 string16 as_string16() const {
76 // StringPiece claims that this is bad when data() is NULL, but unittesting
77 // seems to say otherwise.
78 return string16(data(), size());
79 }
80
81 iterator begin() const { return ptr_; }
82 iterator end() const { return ptr_ + length_; }
83 const_reverse_iterator rbegin() const {
84 return const_reverse_iterator(ptr_ + length_);
85 }
86 const_reverse_iterator rend() const {
87 return const_reverse_iterator(ptr_);
88 }
89
90 size_type max_size() const { return length_; }
91 size_type capacity() const { return length_; }
92
93 static int wordmemcmp(const char16* p, const char16* p2, size_type N) {
94 return c16memcmp(p, p2, N);
95 }
96
97 private:
98 const char16* ptr_;
99 size_type length_;
100 };
101
102 inline bool operator==(const WideStringPiece& x, const WideStringPiece& y) {
103 if (x.size() != y.size())
104 return false;
105
106 return WideStringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
107 }
108
109 inline bool operator!=(const WideStringPiece& x, const WideStringPiece& y) {
110 return !(x == y);
111 }
112
113 inline bool operator<(const WideStringPiece& x, const WideStringPiece& y) {
114 const int r = WideStringPiece::wordmemcmp(
115 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
116 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
117 }
118
119 inline bool operator>(const WideStringPiece& x, const WideStringPiece& y) {
120 return y < x;
121 }
122
123 inline bool operator<=(const WideStringPiece& x, const WideStringPiece& y) {
124 return !(x > y);
125 }
126
127 inline bool operator>=(const WideStringPiece& x, const WideStringPiece& y) {
128 return !(x < y);
129 }
130
131 } // namespace base
132
133 #endif // BASE_STRING_PIECE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698