2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "src/core/SkTSearch.h"
11 #include "include/private/SkMalloc.h"
15 static inline const char* index_into_base(const char*const* base, int index,
18 return *(const char*const*)((const char*)base + index * elemSize);
21 int SkStrSearch(const char*const* base, int count, const char target[],
22 size_t target_len, size_t elemSize)
27 SkASSERT(base != nullptr);
34 int mid = (hi + lo) >> 1;
35 const char* elem = index_into_base(base, mid, elemSize);
37 int cmp = strncmp(elem, target, target_len);
40 else if (cmp > 0 || strlen(elem) > target_len)
46 const char* elem = index_into_base(base, hi, elemSize);
47 int cmp = strncmp(elem, target, target_len);
48 if (cmp || strlen(elem) > target_len)
57 int SkStrSearch(const char*const* base, int count, const char target[],
60 return SkStrSearch(base, count, target, strlen(target), elemSize);
63 int SkStrLCSearch(const char*const* base, int count, const char target[],
64 size_t len, size_t elemSize)
68 SkAutoAsciiToLC tolc(target, len);
70 return SkStrSearch(base, count, tolc.lc(), len, elemSize);
73 int SkStrLCSearch(const char*const* base, int count, const char target[],
76 return SkStrLCSearch(base, count, target, strlen(target), elemSize);
79 //////////////////////////////////////////////////////////////////////////////
81 SkAutoAsciiToLC::SkAutoAsciiToLC(const char str[], size_t len)
83 // see if we need to compute the length
89 // assign lc to our preallocated storage if len is small enough, or allocate
95 lc = (char*)sk_malloc_throw(len + 1);
99 // convert any asii to lower-case. we let non-ascii (utf8) chars pass
101 for (int i = (int)(len - 1); i >= 0; --i) {
103 if ((c & 0x80) == 0) { // is just ascii
111 SkAutoAsciiToLC::~SkAutoAsciiToLC()
113 if (fLC != fStorage) {