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 /** These functions are safe: invalid sequences will return -1; */
     47 int SkUTF8_CountUnichars(const void* utf8, size_t byteLength);
     48 int SkUTF16_CountUnichars(const void* utf16, size_t byteLength);
     49 int SkUTF32_CountUnichars(const void* utf32, size_t byteLength);
     50 
     51 /** This function is safe: invalid UTF8 sequences will return -1
     52  *  When -1 is returned, ptr is unchanged.
     53  *  Precondition: *ptr < end;
     54  */
     55 SkUnichar SkUTF8_NextUnicharWithError(const char** ptr, const char* end);
     56 
     57 /** this version replaces invalid utf-8 sequences with code point U+FFFD. */
     58 inline SkUnichar SkUTF8_NextUnichar(const char** ptr, const char* end) {
     59     SkUnichar val = SkUTF8_NextUnicharWithError(ptr, end);
     60     if (val < 0) {
     61         *ptr = end;
     62         return 0xFFFD;  // REPLACEMENT CHARACTER
     63     }
     64     return val;
     65 }
     66 
     67 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
     68 SkUnichar   SkUTF8_NextUnichar(const char**);
     69 SkUnichar   SkUTF8_PrevUnichar(const char**);
     70 
     71 /** Return the number of bytes need to convert a unichar
     72     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
     73     or 0 if uni is illegal.
     74 */
     75 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = nullptr);
     76 
     77 ///////////////////////////////////////////////////////////////////////////////
     78 
     79 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
     80 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
     81 
     82 int SkUTF16_CountUnichars(const uint16_t utf16[]);
     83 // returns the current unichar and then moves past it (*p++)
     84 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
     85 // this guy backs up to the previus unichar value, and returns it (*--p)
     86 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
     87 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = nullptr);
     88 
     89 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
     90                       char utf8[] = nullptr);
     91 
     92 inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
     93 /*  The 'true' ranges are:
     94  *      0x180B  <= uni <=  0x180D
     95  *      0xFE00  <= uni <=  0xFE0F
     96  *      0xE0100 <= uni <= 0xE01EF
     97  */
     98     if (uni < 0x180B || uni > 0xE01EF) {
     99         return false;
    100     }
    101     if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
    102         return false;
    103     }
    104     return true;
    105 }
    106 
    107 namespace SkHexadecimalDigits {
    108     extern const char gUpper[16];  // 0-9A-F
    109     extern const char gLower[16];  // 0-9a-f
    110 }
    111 
    112 #endif
    113