Skip to content

Commit

Permalink
remove AppendAndFree()
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 23, 2024
1 parent bf87c65 commit bc712d2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/EngineDjVu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,14 +827,14 @@ bool EngineDjVu::ExtractPageText(miniexp_t item, str::WStr& extracted, Vec<Rect>
AppendNewline(extracted, coords, lineSep);
}
const char* content = miniexp_to_str(str);
WCHAR* value = ToWStr(content);
TempWStr value = ToWStrTemp(content);
if (value) {
size_t len = str::Len(value);
// TODO: split the rectangle into individual parts per glyph
for (size_t i = 0; i < len; i++) {
coords.Append(Rect(rect.x, rect.y, rect.dx, rect.dy));
}
extracted.AppendAndFree(value);
extracted.Append(value);
}
if (miniexp_symbol("word") == type) {
extracted.AppendChar(' ');
Expand Down
4 changes: 3 additions & 1 deletion src/EngineEbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,9 @@ class ChmHtmlCollector : public EbookTocVisitor {
}
html.AppendFmt("<pagebreak page_path=\"%s\" page_marker />", plainUrl);
uint charset = ExtractHttpCharset((const char*)pageHtml.Get(), pageHtml.size());
html.AppendAndFree(doc->SmartToUtf8((const char*)pageHtml.Get(), charset));
char* s = doc->SmartToUtf8((const char*)pageHtml.Get(), charset);
html.Append(s);
str::Free(s);
added.Append(plainUrl);
pageHtml.Free();
}
Expand Down
3 changes: 2 additions & 1 deletion src/MobiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ bool MobiDoc::LoadForPdbReader(PdbReader* pdbReader) {
char* docUtf8 = strconv::ToMultiByte(doc->Get(), textEncoding, CP_UTF8);
if (docUtf8) {
doc->Reset();
doc->AppendAndFree(docUtf8);
doc->Append(docUtf8);
str::Free(docUtf8);
}
}
return true;
Expand Down
33 changes: 12 additions & 21 deletions src/utils/StrUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,17 +1582,11 @@ void Str::AppendFmt(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char* res = FmtV(fmt, args);
AppendAndFree(res);
va_end(args);
}

bool Str::AppendAndFree(const char* s) {
if (!s) {
return true;
if (res) {
Append(res);
str::Free(res);
}
bool ok = Append(s, str::Len(s));
str::Free(s);
return ok;
va_end(args);
}

#if 0
Expand Down Expand Up @@ -1923,17 +1917,11 @@ void WStr::AppendFmt(const WCHAR* fmt, ...) {
va_list args;
va_start(args, fmt);
WCHAR* res = FmtV(fmt, args);
AppendAndFree(res);
va_end(args);
}

bool WStr::AppendAndFree(const WCHAR* s) {
if (!s) {
return true;
if (res) {
Append(res);
str::Free(res);
}
bool ok = Append(s, str::Len(s));
str::Free(s);
return ok;
va_end(args);
}

// returns true if was replaced
Expand All @@ -1944,7 +1932,10 @@ bool Replace(WStr& s, const WCHAR* toReplace, const WCHAR* replaceWith) {
}
WCHAR* newStr = str::Replace(s.els, toReplace, replaceWith);
s.Reset();
s.AppendAndFree(newStr);
if (newStr) {
s.Append(newStr);
str::Free(newStr);
}
return true;
}

Expand Down
2 changes: 0 additions & 2 deletions src/utils/StrUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ struct Str {
bool Append(const u8* src, size_t size = -1);
bool AppendSlice(const ByteSlice& d);
void AppendFmt(const char* fmt, ...);
bool AppendAndFree(const char* s);
void Set(const char*);
char* Get() const;
char* CStr() const;
Expand Down Expand Up @@ -403,7 +402,6 @@ struct WStr {
WCHAR& FindEl(const std::function<bool(WCHAR&)>& check) const;
bool IsEmpty() const;
void AppendFmt(const WCHAR* fmt, ...);
bool AppendAndFree(const WCHAR*);
void Set(const WCHAR*);
WCHAR* Get() const;
WCHAR LastChar() const;
Expand Down

0 comments on commit bc712d2

Please sign in to comment.