Some string cleanups.

Also, I think I fixed an accidental memory leak I introduced.
This commit is contained in:
2025-08-11 03:02:26 -04:00
parent 2394e7d9f7
commit 9c4d7b6849
3 changed files with 19 additions and 29 deletions

View File

@ -674,7 +674,6 @@ void Html_tag_content_textarea(DilloHtml *html, const char *tag, int tagsize)
*/ */
void Html_tag_close_textarea(DilloHtml *html) void Html_tag_close_textarea(DilloHtml *html)
{ {
char *str;
int i; int i;
if (html->InFlags & IN_TEXTAREA && !S_TOP(html)->display_none) { if (html->InFlags & IN_TEXTAREA && !S_TOP(html)->display_none) {
@ -696,11 +695,11 @@ void Html_tag_close_textarea(DilloHtml *html)
} }
/* The HTML3.2 spec says it can have "text and character entities". */ /* The HTML3.2 spec says it can have "text and character entities". */
str = a_Html_parse_entities(html, html->Stash->str, html->Stash->len); auto str = a_Html_parse_entities(html, html->Stash->str, html->Stash->len);
auto input = Html_get_current_input(*html); auto input = Html_get_current_input(*html);
if (input) { if (input) {
input->init_str = str; input->init_str = str;
((MultiLineTextResource *)input->embed->getResource ())->setText(str); ((MultiLineTextResource *)input->embed->getResource ())->setText(str.c_str());
} }
} }

View File

@ -1036,20 +1036,20 @@ static const char *Html_parse_entity(DilloHtml *html, const char *token,
* Parse all the entities in a token. Takes the token and its length, and * Parse all the entities in a token. Takes the token and its length, and
* returns a newly allocated string. * returns a newly allocated string.
*/ */
char *a_Html_parse_entities(DilloHtml *html, const char *token, int toksize) std::string
a_Html_parse_entities(DilloHtml *html, const char *token, int toksize)
{ {
const char *esc_set = "&"; const char *esc_set = "&";
int i, s, entsize; int i, s, entsize;
char *str;
s = strcspn(token, esc_set); s = strcspn(token, esc_set);
if (s >= toksize) { if (s >= toksize) {
/* no ampersands */ /* no ampersands */
str = dStrndup(token, toksize); return std::string{ token, token + toksize };
} else { } else {
Dstr *ds = dStr_sized_new(toksize); std::string ds;
ds+= token;
dStr_append_l(ds, token, s); ds+= s;
for (i = s; i < toksize; i++) { for (i = s; i < toksize; i++) {
const char *entstr; const char *entstr;
@ -1058,16 +1058,14 @@ char *a_Html_parse_entities(DilloHtml *html, const char *token, int toksize)
if (token[i] == '&' && if (token[i] == '&' &&
(entstr = Html_parse_entity(html, token+i, toksize-i, &entsize, (entstr = Html_parse_entity(html, token+i, toksize-i, &entsize,
is_attr))) { is_attr))) {
dStr_append(ds, entstr); ds+= entstr;
i += entsize-1; i += entsize-1;
} else { } else {
dStr_append_c(ds, token[i]); ds+= token[i];
} }
} }
str = ds->str; return ds;
dStr_free(ds, 0);
} }
return str;
} }
/** /**
@ -1185,7 +1183,6 @@ static void Html_process_space(DilloHtml *html, const char *space,
static void Html_process_word(DilloHtml *html, const char *word, int size) static void Html_process_word(DilloHtml *html, const char *word, int size)
{ {
int i, j, start; int i, j, start;
char *Pword;
DilloHtmlParseMode parse_mode = S_TOP(html)->parse_mode; DilloHtmlParseMode parse_mode = S_TOP(html)->parse_mode;
if (S_TOP(html)->display_none) if (S_TOP(html)->display_none)
@ -1201,9 +1198,8 @@ static void Html_process_word(DilloHtml *html, const char *word, int size)
dStr_append_c(html->Stash, ' '); dStr_append_c(html->Stash, ' ');
html->StashSpace = false; html->StashSpace = false;
} }
Pword = a_Html_parse_entities(html, word, size); const std::string Pword= a_Html_parse_entities(html, word, size);
dStr_append(html->Stash, Pword); dStr_append(html->Stash, Pword.c_str());
dFree(Pword);
} else if (parse_mode == DILLO_HTML_PARSE_MODE_VERBATIM) { } else if (parse_mode == DILLO_HTML_PARSE_MODE_VERBATIM) {
/* word goes in untouched, it is not processed here. */ /* word goes in untouched, it is not processed here. */
@ -1216,23 +1212,21 @@ static void Html_process_word(DilloHtml *html, const char *word, int size)
} else if (parse_mode == DILLO_HTML_PARSE_MODE_PRE) { } else if (parse_mode == DILLO_HTML_PARSE_MODE_PRE) {
/* all this overhead is to catch white-space entities */ /* all this overhead is to catch white-space entities */
Pword = a_Html_parse_entities(html, word, size); std::string Pword = a_Html_parse_entities(html, word, size);
for (start = i = 0; Pword[i]; start = i) for (start = i = 0; Pword[i]; start = i)
if (isspace(Pword[i])) { if (isspace(Pword[i])) {
while (Pword[++i] && isspace(Pword[i])) ; while (Pword[++i] && isspace(Pword[i])) ;
Html_process_space(html, Pword + start, i - start); Html_process_space(html, Pword.c_str() + start, i - start);
} else { } else {
while (Pword[++i] && !isspace(Pword[i])) ; while (Pword[++i] && !isspace(Pword[i])) ;
HT2TB(html)->addText(Pword + start, i - start, html->wordStyle ()); HT2TB(html)->addText(Pword.c_str() + start, i - start, html->wordStyle ());
html->pre_column += i - start; html->pre_column += i - start;
html->PreFirstChar = false; html->PreFirstChar = false;
} }
dFree(Pword);
} else { } else {
const char *word2, *beyond_word2; const char *word2, *beyond_word2;
Pword = NULL; std::string Pword;
if (!memchr(word,'&', size)) { if (!memchr(word,'&', size)) {
/* No entities */ /* No entities */
word2 = word; word2 = word;
@ -1250,7 +1244,7 @@ static void Html_process_word(DilloHtml *html, const char *word, int size)
; ;
} }
} }
word2 = Pword; word2 = Pword.c_str();
beyond_word2 = word2 + strlen(word2); beyond_word2 = word2 + strlen(word2);
} }
for (start = i = 0; word2[i]; start = i) { for (start = i = 0; word2[i]; start = i) {
@ -1275,8 +1269,6 @@ static void Html_process_word(DilloHtml *html, const char *word, int size)
HT2TB(html)->addText(word2 + start, i - start, html->wordStyle ()); HT2TB(html)->addText(word2 + start, i - start, html->wordStyle ());
} }
} }
if (Pword == word2)
dFree(Pword);
} }
} }
@ -1978,7 +1970,6 @@ static void Html_tag_content_br(DilloHtml *html, const char *tag, int tagsize)
static void Html_tag_open_font(DilloHtml *html, const char *tag, int tagsize) static void Html_tag_open_font(DilloHtml *html, const char *tag, int tagsize)
{ {
const char *attrbuf; const char *attrbuf;
char *fontFamily = NULL;
int32_t color; int32_t color;
if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "color"))) { if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "color"))) {

View File

@ -289,7 +289,7 @@ std::unique_ptr< DilloUrl > a_Html_url_new(DilloHtml *html,
void a_Html_common_image_attrs(DilloHtml *html, const char *tag, int tagsize); void a_Html_common_image_attrs(DilloHtml *html, const char *tag, int tagsize);
std::shared_ptr< DilloImage > a_Html_image_new(DilloHtml *html, const char *tag, int tagsize); std::shared_ptr< DilloImage > a_Html_image_new(DilloHtml *html, const char *tag, int tagsize);
char *a_Html_parse_entities(DilloHtml *html, const char *token, int toksize); std::string a_Html_parse_entities(DilloHtml *html, const char *token, int toksize);
void a_Html_pop_tag(DilloHtml *html, int TagIdx); void a_Html_pop_tag(DilloHtml *html, int TagIdx);
void a_Html_stash_init(DilloHtml *html); void a_Html_stash_init(DilloHtml *html);
int32_t a_Html_color_parse(DilloHtml *html, const char *str, int32_t a_Html_color_parse(DilloHtml *html, const char *str,