More local string cleanups.

This commit is contained in:
2025-08-11 03:36:00 -04:00
parent 17b7c8a8dc
commit f5a870a207

View File

@ -1279,24 +1279,23 @@ char *DilloHtmlFormImpl::makeMultipartBoundary(iconv_t char_encoder,
std::string DilloHtmlFormImpl::encodeText(iconv_t char_encoder, std::string &&input) std::string DilloHtmlFormImpl::encodeText(iconv_t char_encoder, std::string &&input)
{ {
int rc = 0; int rc = 0;
Dstr *output;
const int bufsize = 128; const int bufsize = 128;
inbuf_t *inPtr; inbuf_t *inPtr;
char *buffer, *outPtr; char *outPtr;
size_t inLeft, outRoom; size_t inLeft, outRoom;
bool bad_chars = false; bool bad_chars = false;
if ((char_encoder == (iconv_t) -1) || input.empty()) if ((char_encoder == (iconv_t) -1) || input.empty())
return input; return input;
output = dStr_new(""); std::string output;
inPtr = input.data(); inPtr = input.data();
inLeft = input.size(); inLeft = input.size();
buffer = dNew(char, bufsize); std::vector< char > buffer( bufsize );
while ((rc != EINVAL) && (inLeft > 0)) { while ((rc != EINVAL) && (inLeft > 0)) {
outPtr = buffer; outPtr = buffer.data();
outRoom = bufsize; outRoom = bufsize;
rc = iconv(char_encoder, &inPtr, &inLeft, &outPtr, &outRoom); rc = iconv(char_encoder, &inPtr, &inLeft, &outPtr, &outRoom);
@ -1309,7 +1308,7 @@ std::string DilloHtmlFormImpl::encodeText(iconv_t char_encoder, std::string &&in
// GNU iconv has the undocumented(!) behavior that EILSEQ is also // GNU iconv has the undocumented(!) behavior that EILSEQ is also
// returned when a character cannot be converted. // returned when a character cannot be converted.
dStr_append_l(output, buffer, bufsize - outRoom); output+= std::string_view{ buffer.data(), buffer.data() + bufsize - outRoom };
if (rc == -1) { if (rc == -1) {
rc = errno; rc = errno;
@ -1319,7 +1318,7 @@ std::string DilloHtmlFormImpl::encodeText(iconv_t char_encoder, std::string &&in
bad_chars = true; bad_chars = true;
inPtr++; inPtr++;
inLeft--; inLeft--;
dStr_append_c(output, '?'); output+= '?';
} else if (rc == EINVAL) { } else if (rc == EINVAL) {
MSG_ERR("Form encode text: bad source string.\n"); MSG_ERR("Form encode text: bad source string.\n");
} }
@ -1334,11 +1333,7 @@ std::string DilloHtmlFormImpl::encodeText(iconv_t char_encoder, std::string &&in
MSG_WARN("Form encode text: string cannot be converted cleanly.\n"); MSG_WARN("Form encode text: string cannot be converted cleanly.\n");
} }
dFree(buffer); return output;
std::string rv= output->str;
dStr_free( output, 1 );
return rv;
} }
/** /**