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

Side by Side Diff: include/core/SkTSearch.h

Issue 72603005: Guard against most unintentionally ephemeral SkAutoFoo instantiations. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: docs and REQUIRE_LOCAL_VAR Created 7 years, 1 month 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
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkTSearch_DEFINED 10 #ifndef SkTSearch_DEFINED
11 #define SkTSearch_DEFINED 11 #define SkTSearch_DEFINED
12 12
13 #include "SkTypes.h" 13 #include "SkTypes.h"
14 #include <ctype.h>
14 15
15 /** 16 /**
16 * All of the SkTSearch variants want to return the index (0...N-1) of the 17 * All of the SkTSearch variants want to return the index (0...N-1) of the
17 * found element, or the bit-not of where to insert the element. 18 * found element, or the bit-not of where to insert the element.
18 * 19 *
19 * At a simple level, if the return value is negative, it was not found. 20 * At a simple level, if the return value is negative, it was not found.
20 * 21 *
21 * For clients that want to insert the new element if it was not found, use 22 * For clients that want to insert the new element if it was not found, use
22 * the following logic: 23 * the following logic:
23 * 24 *
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 int SkStrLCSearch(const char*const* base, int count, const char target[], 119 int SkStrLCSearch(const char*const* base, int count, const char target[],
119 size_t elemSize); 120 size_t elemSize);
120 121
121 /** Helper class to convert a string to lower-case, but only modifying the ascii 122 /** Helper class to convert a string to lower-case, but only modifying the ascii
122 characters. This makes the routine very fast and never changes the string 123 characters. This makes the routine very fast and never changes the string
123 length, but it is not suitable for linguistic purposes. Normally this is 124 length, but it is not suitable for linguistic purposes. Normally this is
124 used for buiding and searching string tables. 125 used for buiding and searching string tables.
125 */ 126 */
126 class SkAutoAsciiToLC { 127 class SkAutoAsciiToLC {
127 public: 128 public:
128 SkAutoAsciiToLC(const char str[], size_t len = (size_t)-1); 129 SkAutoAsciiToLC(const char str[], size_t len = (size_t)-1) {
reed1 2013/11/15 18:20:12 Not sure I agree with this one. 1. This class doe
mtklein 2013/11/15 18:28:14 Reverted.
129 ~SkAutoAsciiToLC(); 130 // see if we need to compute the length
131 if ((long)len < 0) {
132 len = strlen(str);
133 }
134 fLength = len;
135
136 // assign lc to our preallocated storage if len is small enough, or allo cate
137 // it on the heap
138 char* lc;
139 if (len <= STORAGE) {
140 lc = fStorage;
141 } else {
142 lc = (char*)sk_malloc_throw(len + 1);
143 }
144 fLC = lc;
145
146 // convert any asii to lower-case. we let non-ascii (utf8) chars pass
147 // through unchanged
148 for (int i = (int)(len - 1); i >= 0; --i) {
149 int c = str[i];
150 if ((c & 0x80) == 0) { // is just ascii
151 c = tolower(c);
152 }
153 lc[i] = c;
154 }
155 lc[len] = 0;
156 }
157 ~SkAutoAsciiToLC() {
158 if (fLC != fStorage) {
159 sk_free(fLC);
160 }
161 }
130 162
131 const char* lc() const { return fLC; } 163 const char* lc() const { return fLC; }
132 size_t length() const { return fLength; } 164 size_t length() const { return fLength; }
133 165
134 private: 166 private:
135 char* fLC; // points to either the heap or fStorage 167 char* fLC; // points to either the heap or fStorage
136 size_t fLength; 168 size_t fLength;
137 enum { 169 enum {
138 STORAGE = 64 170 STORAGE = 64
139 }; 171 };
140 char fStorage[STORAGE+1]; 172 char fStorage[STORAGE+1];
141 }; 173 };
174 #define SkAutoAsciiToLC(...) SK_REQUIRE_LOCAL_VAR(SkAutoAsciiToLC)
142 175
143 // Helper when calling qsort with a compare proc that has typed its arguments 176 // Helper when calling qsort with a compare proc that has typed its arguments
144 #define SkCastForQSort(compare) reinterpret_cast<int (*)(const void*, const void *)>(compare) 177 #define SkCastForQSort(compare) reinterpret_cast<int (*)(const void*, const void *)>(compare)
145 178
146 #endif 179 #endif
OLDNEW
« no previous file with comments | « include/core/SkString.h ('k') | include/core/SkThread.h » ('j') | src/core/SkDraw.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698