aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkUtils.h
diff options
context:
space:
mode:
authorGravatar Hal Canary <halcanary@google.com>2018-07-25 16:52:48 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-07-31 20:11:19 +0000
commitf107a2fd014cd39c489060f2cd1b99cd49c7d0be (patch)
tree5c324821344901869203bbe055be8d3f69f696cb /src/core/SkUtils.h
parent1935aa3d27cd4ed4aef2dc04360f247a541d4b00 (diff)
SkUTF
Create new header and namespace, `SkUTF` where we are putting all of our robust, well documented UTF-8, UTF-16, and UTF-32 functions: `SkUTF::{Count,Next,To}UTF{8,16,32}()`. SkUTF.h and SkUTF.cpp do not depend on the rest of Skia and are suitable for re-use in other modules. Some of the old UTF-{8,16} functions still live in SkUtils.h; their use will be phased out in future CLs. Also added more unit testing and cleaned up old tests. Removed functions that were unused outside of tests or used only once. Change-Id: Iaa59b8705abccf9c4ba082f855da368a0bad8380 Reviewed-on: https://skia-review.googlesource.com/143306 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'src/core/SkUtils.h')
-rw-r--r--src/core/SkUtils.h98
1 files changed, 28 insertions, 70 deletions
diff --git a/src/core/SkUtils.h b/src/core/SkUtils.h
index 795b47a0c4..e18934d6b8 100644
--- a/src/core/SkUtils.h
+++ b/src/core/SkUtils.h
@@ -8,10 +8,9 @@
#ifndef SkUtils_DEFINED
#define SkUtils_DEFINED
-#include "SkTypes.h"
-#include "SkMath.h"
#include "SkOpts.h"
#include "SkTypeface.h"
+#include "../utils/SkUTF.h"
/** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer.
@param buffer The memory to have value copied into it
@@ -27,87 +26,46 @@ static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) {
static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) {
SkOpts::memset64(buffer, value, count);
}
-///////////////////////////////////////////////////////////////////////////////
-#define kMaxBytesInUTF8Sequence 4
+///////////////////////////////////////////////////////////////////////////////
-#ifdef SK_DEBUG
- int SkUTF8_LeadByteToCount(unsigned c);
-#else
- #define SkUTF8_LeadByteToCount(c) ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
-#endif
+// Unlike the functions in SkUTF.h, these two functions do not take an array
+// length parameter. When possible, use SkUTF::NextUTF{8,16} instead.
+SkUnichar SkUTF8_NextUnichar(const char**);
+SkUnichar SkUTF16_NextUnichar(const uint16_t**);
-inline int SkUTF8_CountUTF8Bytes(const char utf8[]) {
- SkASSERT(utf8);
- return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
-}
+///////////////////////////////////////////////////////////////////////////////
-int SkUTF8_CountUnichars(const char utf8[]);
+static inline bool SkUTF16_IsHighSurrogate(uint16_t c) { return ((c) & 0xFC00) == 0xD800; }
-/** These functions are safe: invalid sequences will return -1; */
-int SkUTF8_CountUnichars(const void* utf8, size_t byteLength);
-int SkUTF16_CountUnichars(const void* utf16, size_t byteLength);
-int SkUTF32_CountUnichars(const void* utf32, size_t byteLength);
-int SkUTFN_CountUnichars(SkTypeface::Encoding encoding, const void* utfN, size_t byteLength);
+static inline bool SkUTF16_IsLowSurrogate (uint16_t c) { return ((c) & 0xFC00) == 0xDC00; }
-/** This function is safe: invalid UTF8 sequences will return -1
- * When -1 is returned, ptr is unchanged.
- * Precondition: *ptr < end;
- */
-SkUnichar SkUTF8_NextUnicharWithError(const char** ptr, const char* end);
+///////////////////////////////////////////////////////////////////////////////
-/** this version replaces invalid utf-8 sequences with code point U+FFFD. */
-inline SkUnichar SkUTF8_NextUnichar(const char** ptr, const char* end) {
- SkUnichar val = SkUTF8_NextUnicharWithError(ptr, end);
- if (val < 0) {
- *ptr = end;
- return 0xFFFD; // REPLACEMENT CHARACTER
+static inline int SkUTFN_CountUnichars(SkTypeface::Encoding enc, const void* utfN, size_t bytes) {
+ switch (enc) {
+ case SkTypeface::kUTF8_Encoding: return SkUTF::CountUTF8((const char*)utfN, bytes);
+ case SkTypeface::kUTF16_Encoding: return SkUTF::CountUTF16((const uint16_t*)utfN, bytes);
+ case SkTypeface::kUTF32_Encoding: return SkUTF::CountUTF32((const int32_t*)utfN, bytes);
+ default: SkDEBUGFAIL("unknown text encoding"); return -1;
}
- return val;
}
-SkUnichar SkUTF8_ToUnichar(const char utf8[]);
-SkUnichar SkUTF8_NextUnichar(const char**);
-SkUnichar SkUTF8_PrevUnichar(const char**);
-
-/** Return the number of bytes need to convert a unichar
- into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
- or 0 if uni is illegal.
-*/
-size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = nullptr);
-
-///////////////////////////////////////////////////////////////////////////////
-
-#define SkUTF16_IsHighSurrogate(c) (((c) & 0xFC00) == 0xD800)
-#define SkUTF16_IsLowSurrogate(c) (((c) & 0xFC00) == 0xDC00)
-
-int SkUTF16_CountUnichars(const uint16_t utf16[]);
-// returns the current unichar and then moves past it (*p++)
-SkUnichar SkUTF16_NextUnichar(const uint16_t**);
-SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr, const uint16_t* end);
-
-// this guy backs up to the previus unichar value, and returns it (*--p)
-SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
-size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = nullptr);
-
-size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
- char utf8[] = nullptr);
-
-inline bool SkUnichar_IsVariationSelector(SkUnichar uni) {
-/* The 'true' ranges are:
- * 0x180B <= uni <= 0x180D
- * 0xFE00 <= uni <= 0xFE0F
- * 0xE0100 <= uni <= 0xE01EF
- */
- if (uni < 0x180B || uni > 0xE01EF) {
- return false;
- }
- if ((uni > 0x180D && uni < 0xFE00) || (uni > 0xFE0F && uni < 0xE0100)) {
- return false;
+static inline SkUnichar SkUTFN_Next(SkTypeface::Encoding enc,
+ const void** ptr, const void* stop) {
+ switch (enc) {
+ case SkTypeface::kUTF8_Encoding:
+ return SkUTF::NextUTF8((const char**)ptr, (const char*)stop);
+ case SkTypeface::kUTF16_Encoding:
+ return SkUTF::NextUTF16((const uint16_t**)ptr, (const uint16_t*)stop);
+ case SkTypeface::kUTF32_Encoding:
+ return SkUTF::NextUTF32((const int32_t**)ptr, (const int32_t*)stop);
+ default: SkDEBUGFAIL("unknown text encoding"); return -1;
}
- return true;
}
+///////////////////////////////////////////////////////////////////////////////
+
namespace SkHexadecimalDigits {
extern const char gUpper[16]; // 0-9A-F
extern const char gLower[16]; // 0-9a-f