Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#591: string format functions return new length
Browse files Browse the repository at this point in the history
  • Loading branch information
hugbug committed Jan 12, 2019
1 parent fbfa793 commit c0d7a15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
27 changes: 16 additions & 11 deletions daemon/util/NString.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2015-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -76,18 +76,21 @@ void BString<size>::AppendFmtV(const char* format, va_list ap)
}

template <int size>
void BString<size>::Format(const char* format, ...)
int BString<size>::Format(const char* format, ...)
{
va_list ap;
va_start(ap, format);
FormatV(format, ap);
int len = FormatV(format, ap);
va_end(ap);
return len;
}

template <int size>
void BString<size>::FormatV(const char* format, va_list ap)
int BString<size>::FormatV(const char* format, va_list ap)
{
vsnprintf(m_data, size, format, ap);
// ensure result isn't negative (in case of bad argument)
int len = std::max(vsnprintf(m_data, size, format, ap), 0);
return len;
}

bool CString::operator==(const CString& other)
Expand Down Expand Up @@ -159,26 +162,28 @@ void CString::AppendFmtV(const char* format, va_list ap)
va_end(ap2);
}

void CString::Format(const char* format, ...)
int CString::Format(const char* format, ...)
{
va_list ap;
va_start(ap, format);
FormatV(format, ap);
int len = FormatV(format, ap);
va_end(ap);
return len;
}

void CString::FormatV(const char* format, va_list ap)
int CString::FormatV(const char* format, va_list ap)
{
va_list ap2;
va_copy(ap2, ap);

int newLen = vsnprintf(nullptr, 0, format, ap);
if (newLen < 0) return; // bad argument
// "std::max" to ensure result isn't negative (in case of bad argument)
int newLen = std::max(vsnprintf(nullptr, 0, format, ap), 0);

m_data = (char*)realloc(m_data, newLen + 1);
vsnprintf(m_data, newLen + 1, format, ap2);
newLen = vsnprintf(m_data, newLen + 1, format, ap2);

va_end(ap2);
return newLen;
}

int CString::Find(const char* str, int pos)
Expand Down
10 changes: 5 additions & 5 deletions daemon/util/NString.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2015-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -46,8 +46,8 @@ class BString
void Append(const char* str, int len = 0);
void AppendFmt(const char* format, ...) PRINTF_SYNTAX(2);
void AppendFmtV(const char* format, va_list ap);
void Format(const char* format, ...) PRINTF_SYNTAX(2);
void FormatV(const char* format, va_list ap);
int Format(const char* format, ...) PRINTF_SYNTAX(2);
int FormatV(const char* format, va_list ap);

protected:
char m_data[size];
Expand Down Expand Up @@ -83,8 +83,8 @@ class CString
void Append(const char* str, int len = 0);
void AppendFmt(const char* format, ...) PRINTF_SYNTAX(2);
void AppendFmtV(const char* format, va_list ap);
void Format(const char* format, ...) PRINTF_SYNTAX(2);
void FormatV(const char* format, va_list ap);
int Format(const char* format, ...) PRINTF_SYNTAX(2);
int FormatV(const char* format, va_list ap);
int Find(const char* str, int pos = 0);
void Replace(int pos, int len, const char* str, int strLen = 0);
void Replace(const char* from, const char* to);
Expand Down

0 comments on commit c0d7a15

Please sign in to comment.