OSDN Git Service

[Feature] いくつかの関数の引数を concptr から std::string_view に変更
authorHabu <habu1010+github@gmail.com>
Tue, 21 Sep 2021 08:18:11 +0000 (17:18 +0900)
committerHabu <habu1010+github@gmail.com>
Tue, 21 Sep 2021 10:11:24 +0000 (19:11 +0900)
今後の修正で string_make により作成している文字列変数を std::string に
変更していくにあたり、以下の関数を呼ぶ時にいちいち c_str() メンバ
関数を呼ばなくてよいようにするため、引数の型を concptr から
std::string_view に変更しておく。

- streq
- suffix
- prefix
- text_to_ascii
- ascii_to_text

src/term/z-util.cpp
src/term/z-util.h
src/util/string-processor.cpp
src/util/string-processor.h

index 4cb9693..e23bd69 100644 (file)
@@ -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;
 }
 
 
index 9c24f2e..8036c66 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "system/h-basic.h"
 
+#include <string_view>
 
 /*
  * 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);
-
index 29684d1..e5771f5 100644 (file)
@@ -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) {
index 309760f..410d912 100644 (file)
@@ -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);