From 1e80bce7d2608a6a606447ec527a8193f62ed7ab Mon Sep 17 00:00:00 2001 From: Habu Date: Tue, 21 Sep 2021 17:18:11 +0900 Subject: [PATCH] =?utf8?q?[Feature]=20=E3=81=84=E3=81=8F=E3=81=A4=E3=81=8B?= =?utf8?q?=E3=81=AE=E9=96=A2=E6=95=B0=E3=81=AE=E5=BC=95=E6=95=B0=E3=82=92?= =?utf8?q?=20concptr=20=E3=81=8B=E3=82=89=20std::string=5Fview=20=E3=81=AB?= =?utf8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 今後の修正で string_make により作成している文字列変数を std::string に 変更していくにあたり、以下の関数を呼ぶ時にいちいち c_str() メンバ 関数を呼ばなくてよいようにするため、引数の型を concptr から std::string_view に変更しておく。 - streq - suffix - prefix - text_to_ascii - ascii_to_text --- src/term/z-util.cpp | 33 +++++++++++---------------------- src/term/z-util.h | 9 ++++----- src/util/string-processor.cpp | 6 ++++-- src/util/string-processor.h | 4 ++-- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/term/z-util.cpp b/src/term/z-util.cpp index 4cb9693e3..e23bd69c2 100644 --- a/src/term/z-util.cpp +++ b/src/term/z-util.cpp @@ -22,42 +22,31 @@ concptr argv0 = nullptr; /* * Determine if string "t" is equal to string "t" */ -bool streq(concptr a, concptr b) +bool streq(std::string_view a, std::string_view b) { - return (!strcmp(a, b)); + return a == b; } - /* * Determine if string "t" is a suffix of string "s" */ -bool suffix(concptr s, concptr t) +bool suffix(std::string_view s, std::string_view t) { - int tlen = strlen(t); - int slen = strlen(s); - - /* Check for incompatible lengths */ - if (tlen > slen) return false; + //! @todo C++20 では ends_with が使用可能 + if (t.size() > s.size()) { + return false; + } - /* Compare "t" to the end of "s" */ - return (!strcmp(s + slen - tlen, t)); + return s.compare(s.size() - t.size(), s.npos, t) == 0; } - /* * Determine if string "t" is a prefix of string "s" */ -bool prefix(concptr s, concptr t) +bool prefix(std::string_view s, std::string_view t) { - /* Scan "t" */ - while (*t) - { - /* Compare content and length */ - if (*t++ != *s++) return false; - } - - /* Matched, we have a prefix */ - return true; + //! @todo C++20 では starts_with が使用可能 + return s.substr(0, t.size()) == t; } diff --git a/src/term/z-util.h b/src/term/z-util.h index 9c24f2eaf..8036c66ec 100644 --- a/src/term/z-util.h +++ b/src/term/z-util.h @@ -13,6 +13,7 @@ #include "system/h-basic.h" +#include /* * Extremely basic stuff, like global temp and constant variables. @@ -36,10 +37,9 @@ extern void (*core_aux)(concptr); /**** Available Functions ****/ /* Test equality, prefix, suffix */ -extern bool streq(concptr s, concptr t); -extern bool prefix(concptr s, concptr t); -extern bool suffix(concptr s, concptr t); - +extern bool streq(std::string_view s, std::string_view t); +extern bool prefix(std::string_view s, std::string_view t); +extern bool suffix(std::string_view s, std::string_view t); /* Print an error message */ extern void plog(concptr str); @@ -85,4 +85,3 @@ extern void s64b_mod(int32_t *A1, uint32_t *A2, int32_t B1, uint32_t B2); extern int count_bits(BIT_FLAGS x); extern int mysqrt(int n); - diff --git a/src/util/string-processor.cpp b/src/util/string-processor.cpp index 29684d13e..e5771f511 100644 --- a/src/util/string-processor.cpp +++ b/src/util/string-processor.cpp @@ -181,9 +181,10 @@ static void trigger_text_to_ascii(char **bufptr, concptr *strptr) * parsing "\xFF" into a (signed) char. Whoever thought of making * the "sign" of a "char" undefined is a complete moron. Oh well. */ -void text_to_ascii(char *buf, concptr str) +void text_to_ascii(char *buf, std::string_view sv) { char *s = buf; + auto str = sv.data(); while (*str) { if (*str == '\\') { str++; @@ -305,9 +306,10 @@ static bool trigger_ascii_to_text(char **bufptr, concptr *strptr) /* * Hack -- convert a string into a printable form */ -void ascii_to_text(char *buf, concptr str) +void ascii_to_text(char *buf, std::string_view sv) { char *s = buf; + auto str = sv.data(); while (*str) { byte i = (byte)(*str++); if (i == 31) { diff --git a/src/util/string-processor.h b/src/util/string-processor.h index 309760fde..410d91266 100644 --- a/src/util/string-processor.h +++ b/src/util/string-processor.h @@ -21,8 +21,8 @@ extern concptr macro_modifier_name[MAX_MACRO_MOD]; extern concptr macro_trigger_name[MAX_MACRO_TRIG]; extern concptr macro_trigger_keycode[2][MAX_MACRO_TRIG]; -void text_to_ascii(char *buf, concptr str); -void ascii_to_text(char *buf, concptr str); +void text_to_ascii(char *buf, std::string_view sv); +void ascii_to_text(char *buf, std::string_view sv); size_t angband_strcpy(char *buf, concptr src, size_t bufsize); size_t angband_strcat(char *buf, concptr src, size_t bufsize); char *angband_strstr(concptr haystack, concptr needle); -- 2.11.0