LCOV - code coverage report
Current view: directory - gfx/skia/src/core - SkTSearch.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 90 0 0.0 %
Date: 2012-06-02 Functions: 10 0 0.0 %

       1                 : 
       2                 : /*
       3                 :  * Copyright 2006 The Android Open Source Project
       4                 :  *
       5                 :  * Use of this source code is governed by a BSD-style license that can be
       6                 :  * found in the LICENSE file.
       7                 :  */
       8                 : 
       9                 : 
      10                 : #include "SkTSearch.h"
      11                 : #include <ctype.h>
      12                 : 
      13               0 : static inline const char* index_into_base(const char*const* base, int index,
      14                 :                                           size_t elemSize)
      15                 : {
      16               0 :     return *(const char*const*)((const char*)base + index * elemSize);
      17                 : }
      18                 : 
      19               0 : int SkStrSearch(const char*const* base, int count, const char target[],
      20                 :                 size_t target_len, size_t elemSize)
      21                 : {
      22               0 :     if (count <= 0)
      23               0 :         return ~0;
      24                 : 
      25               0 :     SkASSERT(base != NULL);
      26                 : 
      27               0 :     int lo = 0;
      28               0 :     int hi = count - 1;
      29                 : 
      30               0 :     while (lo < hi)
      31                 :     {
      32               0 :         int mid = (hi + lo) >> 1;
      33               0 :         const char* elem = index_into_base(base, mid, elemSize);
      34                 : 
      35               0 :         int cmp = strncmp(elem, target, target_len);
      36               0 :         if (cmp < 0)
      37               0 :             lo = mid + 1;
      38               0 :         else if (cmp > 0 || strlen(elem) > target_len)
      39               0 :             hi = mid;
      40                 :         else
      41               0 :             return mid;
      42                 :     }
      43                 : 
      44               0 :     const char* elem = index_into_base(base, hi, elemSize);
      45               0 :     int cmp = strncmp(elem, target, target_len);
      46               0 :     if (cmp || strlen(elem) > target_len)
      47                 :     {
      48               0 :         if (cmp < 0)
      49               0 :             hi += 1;
      50               0 :         hi = ~hi;
      51                 :     }
      52               0 :     return hi;
      53                 : }
      54                 : 
      55               0 : int SkStrSearch(const char*const* base, int count, const char target[],
      56                 :                 size_t elemSize)
      57                 : {
      58               0 :     return SkStrSearch(base, count, target, strlen(target), elemSize);
      59                 : }
      60                 : 
      61               0 : int SkStrLCSearch(const char*const* base, int count, const char target[],
      62                 :                   size_t len, size_t elemSize)
      63                 : {
      64               0 :     SkASSERT(target);
      65                 : 
      66               0 :     SkAutoAsciiToLC tolc(target, len);
      67                 : 
      68               0 :     return SkStrSearch(base, count, tolc.lc(), len, elemSize);
      69                 : }
      70                 : 
      71               0 : int SkStrLCSearch(const char*const* base, int count, const char target[],
      72                 :                   size_t elemSize)
      73                 : {
      74               0 :     return SkStrLCSearch(base, count, target, strlen(target), elemSize);
      75                 : }
      76                 : 
      77                 : //////////////////////////////////////////////////////////////////////////////
      78                 : 
      79               0 : SkAutoAsciiToLC::SkAutoAsciiToLC(const char str[], size_t len)
      80                 : {
      81                 :     // see if we need to compute the length
      82               0 :     if ((long)len < 0) {
      83               0 :         len = strlen(str);
      84                 :     }
      85               0 :     fLength = len;
      86                 : 
      87                 :     // assign lc to our preallocated storage if len is small enough, or allocate
      88                 :     // it on the heap
      89                 :     char*   lc;
      90               0 :     if (len <= STORAGE) {
      91               0 :         lc = fStorage;
      92                 :     } else {
      93               0 :         lc = (char*)sk_malloc_throw(len + 1);
      94                 :     }
      95               0 :     fLC = lc;
      96                 :     
      97                 :     // convert any asii to lower-case. we let non-ascii (utf8) chars pass
      98                 :     // through unchanged
      99               0 :     for (int i = (int)(len - 1); i >= 0; --i) {
     100               0 :         int c = str[i];
     101               0 :         if ((c & 0x80) == 0) {   // is just ascii
     102               0 :             c = tolower(c);
     103                 :         }
     104               0 :         lc[i] = c;
     105                 :     }
     106               0 :     lc[len] = 0;
     107               0 : }
     108                 : 
     109               0 : SkAutoAsciiToLC::~SkAutoAsciiToLC()
     110                 : {
     111               0 :     if (fLC != fStorage) {
     112               0 :         sk_free(fLC);
     113                 :     }
     114               0 : }
     115                 : 
     116                 : //////////////////////////////////////////////////////////////////////////////
     117                 : 
     118                 : #define SK_QSortTempSize    16
     119                 : 
     120               0 : static inline void sk_qsort_swap(char a[], char b[], size_t elemSize)
     121                 : {
     122                 :     char    tmp[SK_QSortTempSize];
     123                 : 
     124               0 :     while (elemSize > 0)
     125                 :     {
     126               0 :         size_t size = elemSize;
     127               0 :         if (size > SK_QSortTempSize)
     128               0 :             size = SK_QSortTempSize;
     129               0 :         elemSize -= size;
     130                 : 
     131               0 :         memcpy(tmp, a, size);
     132               0 :         memcpy(a, b, size);
     133               0 :         memcpy(b, tmp, size);
     134               0 :         a += size;
     135               0 :         b += size;
     136                 :     }
     137               0 : }
     138                 : 
     139               0 : static void SkQSort_Partition(char* first, char* last, size_t elemSize, SkQSortCompareProc compare)
     140                 : {
     141               0 :     char*   left = first;
     142               0 :     char*   rite = last;
     143               0 :     char*   pivot = left;
     144                 : 
     145               0 :     while (left <= rite)
     146                 :     {
     147               0 :         while (left < last && compare(left, pivot) < 0)
     148               0 :             left += elemSize;
     149               0 :         while (first < rite && compare(rite, pivot) > 0)
     150               0 :             rite -= elemSize;
     151               0 :         if (left <= rite)
     152                 :         {
     153               0 :             if (left < rite)
     154                 :             {
     155               0 :                 SkASSERT(compare(left, rite) >= 0);
     156               0 :                 sk_qsort_swap(left, rite, elemSize);
     157                 :             }
     158               0 :             left += elemSize;
     159               0 :             rite -= elemSize;
     160                 :         }
     161                 :     }
     162               0 :     if (first < rite)
     163               0 :         SkQSort_Partition(first, rite, elemSize, compare);
     164               0 :     if (left < last)
     165               0 :         SkQSort_Partition(left, last, elemSize, compare);
     166               0 : }
     167                 : 
     168               0 : void SkQSort(void* base, size_t count, size_t elemSize, SkQSortCompareProc compare)
     169                 : {
     170               0 :     SkASSERT(base);
     171               0 :     SkASSERT(compare);
     172               0 :     SkASSERT(elemSize > 0);
     173                 : 
     174               0 :     if (count <= 1)
     175               0 :         return;
     176                 : 
     177               0 :     SkQSort_Partition((char*)base, (char*)base + (count - 1) * elemSize, elemSize, compare);
     178                 : }
     179                 : 

Generated by: LCOV version 1.7