Skip to content
24 changes: 13 additions & 11 deletions src/BinaryDict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,30 @@ void BinaryDict::ConstructBuffer(std::string& keyBuf,
// Write keys and values to buffers
keyBuf.resize(keyTotalLength, '\0');
valueBuf.resize(valueTotalLength, '\0');
char* pKeyBuffer = const_cast<char*>(keyBuf.c_str());
char* pValueBuffer = const_cast<char*>(valueBuf.c_str());
char* pKeyBuffer = keyBuf.data();
char* pValueBuffer = valueBuf.data();
for (const std::unique_ptr<DictEntry>& entry : *lexicon) {
strcpy(pKeyBuffer, entry->Key().c_str());
keyOffset.push_back(pKeyBuffer - keyBuf.c_str());
pKeyBuffer += entry->KeyLength() + 1;
const std::string& key = entry->Key();
strcpy(pKeyBuffer, key.c_str());
keyOffset.push_back(pKeyBuffer - keyBuf.data());
pKeyBuffer += key.length() + 1;
if (entry->NumValues() == 1) {
const auto* svEntry =
static_cast<const SingleValueDictEntry*>(entry.get());
strcpy(pValueBuffer, svEntry->Value().c_str());
valueOffset.push_back(pValueBuffer - valueBuf.c_str());
pValueBuffer += svEntry->Value().length() + 1;
const std::string& val = svEntry->Value();
strcpy(pValueBuffer, val.c_str());
valueOffset.push_back(pValueBuffer - valueBuf.data());
pValueBuffer += val.length() + 1;
} else {
const auto* mvEntry =
static_cast<const MultiValueDictEntry*>(entry.get());
for (const auto& value : mvEntry->Values()) {
strcpy(pValueBuffer, value.c_str());
valueOffset.push_back(pValueBuffer - valueBuf.c_str());
valueOffset.push_back(pValueBuffer - valueBuf.data());
pValueBuffer += value.length() + 1;
}
}
}
assert(keyBuf.c_str() + keyTotalLength == pKeyBuffer);
assert(valueBuf.c_str() + valueTotalLength == pValueBuffer);
assert(keyBuf.data() + keyTotalLength == pKeyBuffer);
assert(valueBuf.data() + valueTotalLength == pValueBuffer);
}
4 changes: 2 additions & 2 deletions src/SerializedValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ void SerializedValues::ConstructBuffer(std::string* valueBuffer,
}
// Write values to the buffer.
valueBuffer->resize(*valueTotalLength, '\0');
char* pValueBuffer = const_cast<char*>(valueBuffer->c_str());
char* pValueBuffer = valueBuffer->data();
for (const std::unique_ptr<DictEntry>& entry : *lexicon) {
for (const auto& value : entry->Values()) {
strcpy(pValueBuffer, value.c_str());
valueBytes->push_back(static_cast<uint16_t>(value.length() + 1));
pValueBuffer += value.length() + 1;
}
}
assert(valueBuffer->c_str() + *valueTotalLength == pValueBuffer);
assert(valueBuffer->data() + *valueTotalLength == pValueBuffer);
}
2 changes: 1 addition & 1 deletion src/UTF8Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class OPENCC_EXPORT UTF8Util {
static std::string FromSubstr(const char* str, size_t length) {
std::string newStr;
newStr.resize(length);
strncpy(const_cast<char*>(newStr.c_str()), str, length);
strncpy(newStr.data(), str, length);
return newStr;
}

Expand Down
1 change: 1 addition & 0 deletions src/tools/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

#include <cstring>
#include <fstream>

#ifdef _WIN32
Expand Down
Loading