From a3e6fe8011067ef22c673342b40b1f924c06b3c1 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 3 May 2018 07:47:25 +1000 Subject: Ensure all 4 byte characters will work in MapRepresentation by using unsigned int. Use variable assignments to avoid casts. --- src/PositionCache.cxx | 25 ++++++++++++++++--------- src/PositionCache.h | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/PositionCache.cxx b/src/PositionCache.cxx index 0af487a1d..88415abfe 100644 --- a/src/PositionCache.cxx +++ b/src/PositionCache.cxx @@ -367,25 +367,28 @@ void LineLayoutCache::Dispose(LineLayout *ll) { } // Simply pack the (maximum 4) character bytes into an int -static inline int KeyFromString(const char *charBytes, size_t len) { +static unsigned int KeyFromString(const char *charBytes, size_t len) { PLATFORM_ASSERT(len <= 4); - int k=0; + unsigned int k=0; for (size_t i=0; i(charBytes[i]); + const unsigned char uc = charBytes[i]; + k += uc; } return k; } SpecialRepresentations::SpecialRepresentations() { - std::fill(startByteHasReprs, std::end(startByteHasReprs), static_cast(0)); + const short none = 0; + std::fill(startByteHasReprs, std::end(startByteHasReprs), none); } void SpecialRepresentations::SetRepresentation(const char *charBytes, const char *value) { MapRepresentation::iterator it = mapReprs.find(KeyFromString(charBytes, UTF8MaxBytes)); if (it == mapReprs.end()) { // New entry so increment for first byte - startByteHasReprs[static_cast(charBytes[0])]++; + const unsigned char ucStart = charBytes[0]; + startByteHasReprs[ucStart]++; } mapReprs[KeyFromString(charBytes, UTF8MaxBytes)] = Representation(value); } @@ -394,13 +397,15 @@ void SpecialRepresentations::ClearRepresentation(const char *charBytes) { MapRepresentation::iterator it = mapReprs.find(KeyFromString(charBytes, UTF8MaxBytes)); if (it != mapReprs.end()) { mapReprs.erase(it); - startByteHasReprs[static_cast(charBytes[0])]--; + const unsigned char ucStart = charBytes[0]; + startByteHasReprs[ucStart]--; } } const Representation *SpecialRepresentations::RepresentationFromCharacter(const char *charBytes, size_t len) const { PLATFORM_ASSERT(len <= 4); - if (!startByteHasReprs[static_cast(charBytes[0])]) + const unsigned char ucStart = charBytes[0]; + if (!startByteHasReprs[ucStart]) return 0; MapRepresentation::const_iterator it = mapReprs.find(KeyFromString(charBytes, len)); if (it != mapReprs.end()) { @@ -411,7 +416,8 @@ const Representation *SpecialRepresentations::RepresentationFromCharacter(const bool SpecialRepresentations::Contains(const char *charBytes, size_t len) const { PLATFORM_ASSERT(len <= 4); - if (!startByteHasReprs[static_cast(charBytes[0])]) + const unsigned char ucStart = charBytes[0]; + if (!startByteHasReprs[ucStart]) return false; MapRepresentation::const_iterator it = mapReprs.find(KeyFromString(charBytes, len)); return it != mapReprs.end(); @@ -419,7 +425,8 @@ bool SpecialRepresentations::Contains(const char *charBytes, size_t len) const { void SpecialRepresentations::Clear() { mapReprs.clear(); - std::fill(startByteHasReprs, std::end(startByteHasReprs), static_cast(0)); + const short none = 0; + std::fill(startByteHasReprs, std::end(startByteHasReprs), none); } void BreakFinder::Insert(int val) { diff --git a/src/PositionCache.h b/src/PositionCache.h index c294edebd..c1ecd2c0f 100644 --- a/src/PositionCache.h +++ b/src/PositionCache.h @@ -160,7 +160,7 @@ public: } }; -typedef std::map MapRepresentation; +typedef std::map MapRepresentation; class SpecialRepresentations { MapRepresentation mapReprs; -- cgit v1.2.3