Skip to content

Commit

Permalink
UString: At() と SubMatch() を実装。
Browse files Browse the repository at this point in the history
  • Loading branch information
isaki68k committed Oct 14, 2023
1 parent c3ffc36 commit 314d884
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/UString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ UString::Init(const std::string& codeset)
}
}

// pos 文字目から key.size() 文字が key と一致すれば true を返す。
bool
UString::SubMatch(size_type pos, const std::string& key) const
{
size_type len = key.size();

if (pos + len >= size()) {
return false;
}
for (size_type i = 0; i < len; i++) {
if ((*this)[pos + i] != key[i]) {
return false;
}
}
return true;
}

// UTF-8 文字列 str を UString に変換する。
/*static*/ UString
UString::FromUTF8(const std::string& str)
Expand Down
13 changes: 13 additions & 0 deletions src/UString.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ class UString : public std::vector<unichar>
return *this;
}

// pos 文字目を返す。pos が終端以降を指していれば 0 を返す。
unichar At(size_type pos) const
{
if (__predict_true(pos < size())) {
return (*this)[pos];
} else {
return 0;
}
}

// pos 文字目から key.length() 文字が key と一致すれば true を返す。
bool SubMatch(size_type pos, const std::string& key) const;

// UTF-8 文字列 str を UString に変換する
static UString FromUTF8(const std::string& str);

Expand Down

0 comments on commit 314d884

Please sign in to comment.