1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkTSearch.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include "SkTSearch.h" 1.14 +#include <ctype.h> 1.15 + 1.16 +static inline const char* index_into_base(const char*const* base, int index, 1.17 + size_t elemSize) 1.18 +{ 1.19 + return *(const char*const*)((const char*)base + index * elemSize); 1.20 +} 1.21 + 1.22 +int SkStrSearch(const char*const* base, int count, const char target[], 1.23 + size_t target_len, size_t elemSize) 1.24 +{ 1.25 + if (count <= 0) 1.26 + return ~0; 1.27 + 1.28 + SkASSERT(base != NULL); 1.29 + 1.30 + int lo = 0; 1.31 + int hi = count - 1; 1.32 + 1.33 + while (lo < hi) 1.34 + { 1.35 + int mid = (hi + lo) >> 1; 1.36 + const char* elem = index_into_base(base, mid, elemSize); 1.37 + 1.38 + int cmp = strncmp(elem, target, target_len); 1.39 + if (cmp < 0) 1.40 + lo = mid + 1; 1.41 + else if (cmp > 0 || strlen(elem) > target_len) 1.42 + hi = mid; 1.43 + else 1.44 + return mid; 1.45 + } 1.46 + 1.47 + const char* elem = index_into_base(base, hi, elemSize); 1.48 + int cmp = strncmp(elem, target, target_len); 1.49 + if (cmp || strlen(elem) > target_len) 1.50 + { 1.51 + if (cmp < 0) 1.52 + hi += 1; 1.53 + hi = ~hi; 1.54 + } 1.55 + return hi; 1.56 +} 1.57 + 1.58 +int SkStrSearch(const char*const* base, int count, const char target[], 1.59 + size_t elemSize) 1.60 +{ 1.61 + return SkStrSearch(base, count, target, strlen(target), elemSize); 1.62 +} 1.63 + 1.64 +int SkStrLCSearch(const char*const* base, int count, const char target[], 1.65 + size_t len, size_t elemSize) 1.66 +{ 1.67 + SkASSERT(target); 1.68 + 1.69 + SkAutoAsciiToLC tolc(target, len); 1.70 + 1.71 + return SkStrSearch(base, count, tolc.lc(), len, elemSize); 1.72 +} 1.73 + 1.74 +int SkStrLCSearch(const char*const* base, int count, const char target[], 1.75 + size_t elemSize) 1.76 +{ 1.77 + return SkStrLCSearch(base, count, target, strlen(target), elemSize); 1.78 +} 1.79 + 1.80 +////////////////////////////////////////////////////////////////////////////// 1.81 + 1.82 +SkAutoAsciiToLC::SkAutoAsciiToLC(const char str[], size_t len) 1.83 +{ 1.84 + // see if we need to compute the length 1.85 + if ((long)len < 0) { 1.86 + len = strlen(str); 1.87 + } 1.88 + fLength = len; 1.89 + 1.90 + // assign lc to our preallocated storage if len is small enough, or allocate 1.91 + // it on the heap 1.92 + char* lc; 1.93 + if (len <= STORAGE) { 1.94 + lc = fStorage; 1.95 + } else { 1.96 + lc = (char*)sk_malloc_throw(len + 1); 1.97 + } 1.98 + fLC = lc; 1.99 + 1.100 + // convert any asii to lower-case. we let non-ascii (utf8) chars pass 1.101 + // through unchanged 1.102 + for (int i = (int)(len - 1); i >= 0; --i) { 1.103 + int c = str[i]; 1.104 + if ((c & 0x80) == 0) { // is just ascii 1.105 + c = tolower(c); 1.106 + } 1.107 + lc[i] = c; 1.108 + } 1.109 + lc[len] = 0; 1.110 +} 1.111 + 1.112 +SkAutoAsciiToLC::~SkAutoAsciiToLC() 1.113 +{ 1.114 + if (fLC != fStorage) { 1.115 + sk_free(fLC); 1.116 + } 1.117 +}