さて、私は文字列が空であるか空白だけであるかを確認する方法についてStackOverflowを検索しました。ただし、ANSI文字列でのみ機能します。どうすればそれを動作させることができwstring
ますか?
コードは次のとおりです。
#include <string>
using namespace std;
//! Checks if a string is empty or is whitespace.
bool IsEmptyOrSpace(const string& str) {
string::const_iterator it = str.begin();
do {
if (it == str.end())
return true;
} while (*it >= 0 && *it <= 0x7f && isspace(*(it++)));
// One of these conditions will be optimized away by the compiler.
// Which one depends on whether the characters are signed or not.
return false;
}
私の最初の考えはに変更isspace(*(it++))
することでしiswspace(*(it++))
たが、その前の2つの条件はASCIIでのみ機能しますよね?これが私がこれまでに関数をwstring
'sに適応させようとしてきたことです:
bool IsEmptyOrSpaceW(const wstring& str) {
String::const_iterator it = str.begin();
do {
if (it == str.end())
return true;
} while (*it >= 0 && *it <= 0x7f && iswspace(*(it++)));
// One of these conditions will be optimized away by the compiler.
// Which one depends on whether the characters are signed or not.
// Do I need to change "*it >= 0 && *it <= 0x7f" to something else?
return false;
}
私のアプローチは正しいに近いですか?IsEmptyOrSpace()
いずれにせよ、この関数のUnicodeバージョンを実装するにはどうすればよいですか?
編集:
さて、あなたがテストがそこにある理由を知る必要があるならば*it >= 0 && *it <= 0x7f
、私は知らないので、私はあなたに言うことができません。この質問への回答から関数のコードを取得しました:C ++は、文字列がスペースかnullか
を確認します。最初から始めましょう。一般に、awstring
が空か空白かを確認するにはどうすればよいですか?