Skip to content

Commit

Permalink
例外を投げる構造をやめる
Browse files Browse the repository at this point in the history
  • Loading branch information
berryzplus committed Feb 27, 2022
1 parent faad1e4 commit 04ae8aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 61 deletions.
64 changes: 22 additions & 42 deletions sakura_core/util/string_ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@

int __cdecl my_internal_icmp( const char *s1, const char *s2, unsigned int n, unsigned int dcount, bool flag );

/*!
* va_list型のスマートポインタを実現するためのdeleterクラス
*/
struct vaList_ender
{
void operator()(va_list argList) const
{
va_end(argList);
}
};

//! va_list型のスマートポインタ
using vaListHolder = std::unique_ptr<std::remove_pointer<va_list>::type, vaList_ender>;


// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
// 文字 //
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
Expand Down Expand Up @@ -266,12 +251,13 @@ const char* stristr_j( const char* s1, const char* s2 )
@returns 出力された文字数。NUL終端を含まない。
@retval >= 0 正常終了
@retval < 0 異常終了
*/
*/
int vstrprintf(std::wstring& strOut, const WCHAR* pszFormat, va_list& argList)
{
// _vscwprintf() はフォーマットに必要な文字数を返す。
const int cchOut = ::_vscwprintf(pszFormat, argList);
if (cchOut <= 0) {
strOut.clear();
return cchOut;
}

Expand Down Expand Up @@ -307,12 +293,13 @@ int vstrprintf(std::wstring& strOut, const WCHAR* pszFormat, va_list& argList)
@returns 出力された文字数。NUL終端を含まない。
@retval >= 0 正常終了
@retval < 0 異常終了
*/
*/
int vstrprintf(std::string& strOut, const CHAR* pszFormat, va_list& argList)
{
// _vscwprintf() はフォーマットに必要な文字数を返す。
const int cchOut = ::_vscprintf(pszFormat, argList);
if (cchOut <= 0) {
strOut.clear();
return cchOut;
}

Expand All @@ -336,7 +323,6 @@ int vstrprintf(std::string& strOut, const CHAR* pszFormat, va_list& argList)
strOut.assign(strOut.data(), cchOut);
}


return cchOut;
}

Expand All @@ -349,7 +335,7 @@ int vstrprintf(std::string& strOut, const CHAR* pszFormat, va_list& argList)
@returns 出力された文字数。NUL終端を含まない。
@retval >= 0 正常終了
@retval < 0 異常終了
*/
*/
int strprintf(std::wstring& strOut, const WCHAR* pszFormat, ...)
{
va_list argList;
Expand All @@ -371,7 +357,7 @@ int strprintf(std::wstring& strOut, const WCHAR* pszFormat, ...)
@returns 出力された文字数。NUL終端を含まない。
@retval >= 0 正常終了
@retval < 0 異常終了
*/
*/
int strprintf(std::string& strOut, const CHAR* pszFormat, ...)
{
va_list argList;
Expand All @@ -390,15 +376,14 @@ int strprintf(std::string& strOut, const CHAR* pszFormat, ...)
@param[in] pszFormat フォーマット文字列
@param[in] ... 引数リスト
@returns フォーマットされた文字列
@throws invalid_argument フォーマット文字列が正しくないとき
*/
*/
std::wstring vstrprintf(const WCHAR* pszFormat, va_list& argList)
{
// 出力バッファを確保する
std::wstring strOut;

// 戻り値が0未満ならエラーだが、再現させられないのでハンドルしない
vstrprintf(strOut, pszFormat, argList);
const auto nRet = vstrprintf(strOut, pszFormat, argList);
assert(nRet >= 0);

return strOut;
}
Expand All @@ -409,21 +394,14 @@ std::wstring vstrprintf(const WCHAR* pszFormat, va_list& argList)
@param[in] pszFormat フォーマット文字列
@param[in] ... 引数リスト
@returns フォーマットされた文字列
@throws invalid_argument フォーマット文字列が正しくないとき
*/
*/
std::string vstrprintf(const CHAR* pszFormat, va_list& argList)
{
// 出力バッファを確保する
std::string strOut;

// 戻り値が0未満ならエラー
if (const int nRet = vstrprintf(strOut, pszFormat, argList);
0 > nRet)
{
std::array<char, 1024> msg;
::strerror_s(msg.data(), msg.size(), nRet);
throw std::invalid_argument(msg.data());
}
const int nRet = vstrprintf(strOut, pszFormat, argList);
assert(nRet >= 0);

return strOut;
}
Expand All @@ -434,16 +412,17 @@ std::string vstrprintf(const CHAR* pszFormat, va_list& argList)
@param[in] pszFormat フォーマット文字列
@param[in] ... 引数リスト
@returns フォーマットされた文字列
@throws invalid_argument フォーマット文字列が正しくないとき
*/
*/
std::wstring strprintf(const WCHAR* pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);

vaListHolder holder(argList);
const auto strOut = vstrprintf(pszFormat, argList);

return vstrprintf(pszFormat, argList);
va_end(argList);

return strOut;
}

/*!
Expand All @@ -452,16 +431,17 @@ std::wstring strprintf(const WCHAR* pszFormat, ...)
@param[in] pszFormat フォーマット文字列
@param[in] ... 引数リスト
@returns フォーマットされた文字列
@throws invalid_argument フォーマット文字列が正しくないとき
*/
*/
std::string strprintf(const CHAR* pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);

vaListHolder holder(argList);
const auto strOut = vstrprintf(pszFormat, argList);

va_end(argList);

return vstrprintf(pszFormat, argList);
return strOut;
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- //
Expand Down
19 changes: 0 additions & 19 deletions tests/unittests/test-string_ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,6 @@ TEST(string_ex, strprintfA_small_output)
EXPECT_STREQ("1234567890123456", text.c_str());
}

/*!
@brief 独自定義のフォーマット関数(C-Style風)。
Cロケールを設定し忘れた場合、SJISバイナリから標準文字列への変換は失敗する。
テスト作成時に混乱する可能性があるので、例外を投げるようにしてある。
CRT関数が「フォーマットが不正ならクラッシュさせる」という設計なので、
フォーマットチェック機構としては役立たずである点に注意すること。
*/
TEST(string_ex, strprintfA_throws)
{
// Cのロケールを設定し忘れた場合、エラーが返る
setlocale(LC_ALL, "English");
EXPECT_THROW(strprintf("%ls", L"てすと"), std::invalid_argument);

// Cのロケールを日本語にした場合、正しく変換できる
setlocale(LC_ALL, "Japanese");
EXPECT_STREQ("てすと", strprintf("%ls", L"てすと").data());
}

/*!
@brief 独自定義の文字列比較関数。
*/
Expand Down

0 comments on commit 04ae8aa

Please sign in to comment.