[
https://issues.apache.org/jira/browse/GEODE-3288?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16282282#comment-16282282
]
ASF GitHub Bot commented on GEODE-3288:
---------------------------------------
pivotal-jbarrett commented on a change in pull request #165: GEODE-3288:
Converts CacheableString to std::string.
URL: https://github.com/apache/geode-native/pull/165#discussion_r155603226
##########
File path: cppcache/src/CacheableString.cpp
##########
@@ -111,219 +115,61 @@ bool CacheableString::operator==(const CacheableKey&
other) const {
}
}
- const CacheableString& otherStr = static_cast<const CacheableString&>(other);
- // check for lengths first
- if (m_len != otherStr.m_len) {
- return false;
- }
- if (m_str == nullptr) {
- return true;
- } else if (thisType == GF_STRING || thisType == GF_STRING_HUGE) {
- if (otherType == GF_STRING || otherType == GF_STRING_HUGE) {
- return compareStrings(reinterpret_cast<const char*>(m_str),
- reinterpret_cast<const char*>(otherStr.m_str));
- } else {
- return compareStrings(reinterpret_cast<const char*>(m_str),
- reinterpret_cast<const wchar_t*>(otherStr.m_str));
- }
- } else {
- if (otherType == GF_STRING || otherType == GF_STRING_HUGE) {
- return compareStrings(reinterpret_cast<const wchar_t*>(m_str),
- reinterpret_cast<const char*>(otherStr.m_str));
- } else {
- return compareStrings(reinterpret_cast<const wchar_t*>(m_str),
- reinterpret_cast<const wchar_t*>(otherStr.m_str));
- }
- }
+ auto&& otherStr = static_cast<const CacheableString&>(other);
+ return m_str == otherStr.m_str;
}
int32_t CacheableString::hashcode() const {
- if (m_str == nullptr) {
- return 0;
- }
- int localHash = 0;
-
if (m_hashcode == 0) {
- uint32_t prime = 31;
- if (isCString()) {
- const char* data = reinterpret_cast<const char*>(m_str);
- for (uint32_t i = 0; i < m_len; i++) {
- localHash = prime * localHash + data[i];
- }
- } else {
- GF_DEV_ASSERT(isWideString());
- const wchar_t* data = reinterpret_cast<const wchar_t*>(m_str);
- for (uint32_t i = 0; i < m_len; i++) {
- localHash = prime * localHash + data[i];
- }
- }
- m_hashcode = localHash;
+ m_hashcode = geode_hash<decltype(m_str)>{}(m_str);
}
return m_hashcode;
}
-/** Private method to get ASCII char for wide-char if possible */
-inline char getASCIIChar(const wchar_t wc, bool& isASCII, int32_t& encodedLen)
{
- if (wc & 0xfc00) {
- // three bytes required.
- encodedLen += 3;
- isASCII = false;
- } else if (wc & 0x0380) {
- // two byte.
- encodedLen += 2;
- isASCII = false;
- } else {
- // one byte.
- ++encodedLen;
- return (wc & 0x7f);
- }
- return 0;
-}
-
-char* CacheableString::getASCIIString(const wchar_t* value, int32_t& len,
- int32_t& encodedLen) {
- wchar_t currentChar;
- char* buf = nullptr;
- bool isASCII = true;
- char c;
- if (len > 0) {
- int32_t clen = len;
- buf = new char[clen + 1];
- while (clen > 0 && (currentChar = *value) != 0) {
- c = getASCIIChar(currentChar, isASCII, encodedLen);
- if (isASCII) {
- buf[encodedLen - 1] = c;
- }
- ++value, --clen;
- }
- if (isASCII) {
- buf[encodedLen] = '\0';
- } else {
- delete[] buf;
- buf = nullptr;
- }
- len -= clen;
- } else {
- DataOutputInternal out;
- const wchar_t* pvalue = value;
- while ((currentChar = *pvalue) != 0) {
- c = getASCIIChar(currentChar, isASCII, encodedLen);
- if (isASCII) {
- out.write(static_cast<int8_t>(c));
- }
- ++pvalue;
- }
- len = static_cast<int32_t>(pvalue - value);
- if (isASCII) {
- uint32_t sz;
- const uint8_t* outBuf = out.getBuffer(&sz);
- buf = new char[sz + 1];
- memcpy(buf, outBuf, sz);
- buf[sz] = '\0';
- }
+void CacheableString::initString(const char* value, int32_t len) {
+ if (value) {
+ initString(len > 0 ? std::string(value, len) : std::string(value));
}
- return buf;
}
-void CacheableString::copyString(const char* value, int32_t len) {
- GF_NEW(m_str, char[len + 1]);
- memcpy(m_str, value, len * sizeof(char));
- (reinterpret_cast<char*>(m_str))[len] = '\0';
-}
+void CacheableString::initString(std::string&& value) {
+ m_str = std::move(value);
-void CacheableString::copyString(const wchar_t* value, int32_t len) {
- GF_NEW(m_str, wchar_t[len + 1]);
- memcpy(m_str, value, len * sizeof(wchar_t));
- (reinterpret_cast<wchar_t*>(m_str))[len] = L'\0';
-}
+ bool ascii = isAscii(m_str);
-void CacheableString::initString(const char* value, int32_t len) {
- if (value != nullptr) {
- if (len <= 0) {
- len = static_cast<int32_t>(strlen(value));
- }
- if (len > 0xFFFF) {
- m_type = GF_STRING_HUGE;
- }
- m_len = len;
- copyString(value, len);
- }
+ m_type = m_str.length() > std::numeric_limits<uint16_t>::max()
+ ? ascii ? GF_STRING_HUGE : GF_WIDESTRING_HUGE
+ : ascii ? GF_STRING : GF_WIDESTRING;
Review comment:
Prefere this?
```c++
if (m_str.length() > std::numeric_limits<uint16_t>::max()) {
if (ascii) {
m_type = GF_STRING_HUGE;
} else {
m_type = GF_WIDESTRING_HUGE;
} else {
if (ascii) {
m_type = GF_STRING;
} else {
m_type = GF_WIDESTRING;
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Replace char* with std::string
> ------------------------------
>
> Key: GEODE-3288
> URL: https://issues.apache.org/jira/browse/GEODE-3288
> Project: Geode
> Issue Type: Improvement
> Components: native client
> Reporter: Ernest Burghardt
>
> In all public API headers
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)