Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(765)

Unified Diff: src/pdf/SkPDFUtils.cpp

Issue 2099463002: SkPDF: alloc less memory for strings (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-06-23 (Thursday) 16:23:34 EDT Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/pdf/SkPDFUtils.cpp
diff --git a/src/pdf/SkPDFUtils.cpp b/src/pdf/SkPDFUtils.cpp
index f305765389685625d3bc7d5a8310077d129f1bc6..4e1a6d792da20fd8c04f8e36d0d5de90551fbfb7 100644
--- a/src/pdf/SkPDFUtils.cpp
+++ b/src/pdf/SkPDFUtils.cpp
@@ -370,46 +370,48 @@ size_t SkPDFUtils::FloatToDecimal(float value,
return output - result;
}
-SkString SkPDFUtils::FormatString(const char* cin, size_t len) {
+void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) {
SkDEBUGCODE(static const size_t kMaxLen = 65535;)
SkASSERT(len <= kMaxLen);
- // 7-bit clean is a heuristic to decide what string format to use;
- // a 7-bit clean string should require little escaping.
- bool sevenBitClean = true;
- size_t characterCount = 2 + len;
+ size_t extraCharacterCount = 0;
for (size_t i = 0; i < len; i++) {
if (cin[i] > '~' || cin[i] < ' ') {
- sevenBitClean = false;
- break;
+ extraCharacterCount += 3;
}
if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') {
- ++characterCount;
+ ++extraCharacterCount;
}
}
- SkString result;
- if (sevenBitClean) {
- result.resize(characterCount);
- char* str = result.writable_str();
- *str++ = '(';
+ if (extraCharacterCount <= len) {
+ wStream->writeText("(");
for (size_t i = 0; i < len; i++) {
- if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') {
- *str++ = '\\';
+ if (cin[i] > '~' || cin[i] < ' ') {
+ uint8_t c = static_cast<uint8_t>(cin[i]);
+ uint8_t octal[4];
+ octal[0] = '\\';
+ octal[1] = '0' + ( c >> 6 );
+ octal[2] = '0' + ((c >> 3) & 0x07);
+ octal[3] = '0' + ( c & 0x07);
+ wStream->write(octal, 4);
+ } else {
+ if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') {
+ wStream->writeText("\\");
+ }
+ wStream->write(&cin[i], 1);
}
- *str++ = cin[i];
}
- *str++ = ')';
+ wStream->writeText(")");
} else {
- result.resize(2 * len + 2);
- char* str = result.writable_str();
- *str++ = '<';
+ wStream->writeText("<");
for (size_t i = 0; i < len; i++) {
uint8_t c = static_cast<uint8_t>(cin[i]);
static const char gHex[] = "0123456789ABCDEF";
- *str++ = gHex[(c >> 4) & 0xF];
- *str++ = gHex[(c ) & 0xF];
+ char hexValue[2];
+ hexValue[0] = gHex[(c >> 4) & 0xF];
+ hexValue[1] = gHex[ c & 0xF];
+ wStream->write(hexValue, 2);
}
- *str++ = '>';
+ wStream->writeText(">");
}
- return result;
}
« src/pdf/SkPDFDevice.cpp ('K') | « src/pdf/SkPDFUtils.h ('k') | tests/PDFPrimitivesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698