Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2006 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkUtils_DEFINED
      9 #define SkUtils_DEFINED
     10 
     11 #include "SkTypes.h"
     12 #include "SkMath.h"
     13 #include "SkOpts.h"
     14 
     15 /** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer.
     16     @param buffer   The memory to have value copied into it
     17     @param value    The value to be copied into buffer
     18     @param count    The number of times value should be copied into the buffer.
     19 */
     20 static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) {
     21     SkOpts::memset16(buffer, value, count);
     22 }
     23 static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) {
     24     SkOpts::memset32(buffer, value, count);
     25 }
     26 static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) {
     27     SkOpts::memset64(buffer, value, count);
     28 }
     29 ///////////////////////////////////////////////////////////////////////////////
     30 
     31 #define kMaxBytesInUTF8Sequence     4
     32 
     33 #ifdef SK_DEBUG
     34     int SkUTF8_LeadByteToCount(unsigned c);
     35 #else
     36     #define SkUTF8_LeadByteToCount(c)   ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
     37 #endif
     38 
     39 inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
     40     SkASSERT(utf8);
     41     return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
     42 }
     43 
     44 int         SkUTF8_CountUnichars(const char utf8[]);
     45 
     46 /** This function is safe: invalid UTF8 sequences will return -1; */
     47 int         SkUTF8_CountUnicharsWithError(const char utf8[], size_t byteLength);
     48 
     49 /** This function is safe: invalid UTF8 sequences will return 0; */
     50 inline int  SkUTF8_CountUnichars(const char utf8[], size_t byteLength) {
     51     return SkClampPos(SkUTF8_CountUnicharsWithError(utf8, byteLength));
     52 }
     53 
     54 /** This function is safe: invalid UTF8 sequences will return -1
     55  *  When -1 is returned, ptr is unchanged.
     56  *  Precondition: *ptr < end;
     57  */
     58 SkUnichar SkUTF8_NextUnicharWithError(const char** ptr, const char* end);
     59 
     60 /** this version replaces invalid utf-8 sequences with code point U+FFFD. */
     61 inline SkUnichar SkUTF8_NextUnichar(const char** ptr, const char* end) {
     62     SkUnichar val = SkUTF8_NextUnicharWithError(ptr, end);
     63     if (val < 0) {
     64         *ptr = end;
     65         return 0xFFFD;  // REPLACEMENT CHARACTER
     66     }
     67     return val;
     68 }
     69 
     70 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
     71 SkUnichar   SkUTF8_NextUnichar(const char**);
     72 SkUnichar   SkUTF8_PrevUnichar(const char**);
     73 
     74 /** Return the number of bytes need to convert a unichar
     75     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
     76     or 0 if uni is illegal.
     77 */
     78 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
     79 
     80 ///////////////////////////////////////////////////////////////////////////////
     81 
     82 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
     83 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
     84 
     85 int SkUTF16_CountUnichars(const uint16_t utf16[]);
     86 int SkUTF16_CountUnichars(const uint16_t utf16[], int numberOf16BitValues);
     87 // returns the current unichar and then moves past it (*p++)
     88 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
     89 // this guy backs up to the previus unichar value, and returns it (*--p)
     90 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
     91 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
     92 
     93 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
     94                       char utf8[] = NULL);
     95 
     96 inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
     97 /*  The 'true' ranges are:
     98  *      0x180B  <= uni <=  0x180D
     99  *      0xFE00  <= uni <=  0xFE0F
    100  *      0xE0100 <= uni <= 0xE01EF
    101  */
    102     if (uni < 0x180B || uni > 0xE01EF) {
    103         return false;
    104     }
    105     if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
    106         return false;
    107     }
    108     return true;
    109 }
    110 
    111 namespace SkHexadecimalDigits {
    112     extern const char gUpper[16];  // 0-9A-F
    113     extern const char gLower[16];  // 0-9a-f
    114 }
    115 
    116 #endif
    117