OSDN Git Service

Refactor get_table_{name,name_aux,sindarin,sindarin_aux}() and sindarin_to_kana(...
authorEric Branlund <ebranlund@fastmail.com>
Fri, 23 Dec 2022 15:57:27 +0000 (08:57 -0700)
committerEric Branlund <ebranlund@fastmail.com>
Fri, 23 Dec 2022 15:57:27 +0000 (08:57 -0700)
src/artifact/random-art-characteristics.cpp
src/artifact/random-art-generator.cpp
src/flavor/object-flavor.cpp
src/flavor/object-flavor.h
src/locale/japanese.cpp
src/locale/japanese.h

index 3ed1b15..b8c84ff 100644 (file)
@@ -143,23 +143,21 @@ std::string get_random_name(const ItemEntity &item, bool armour, int power)
 {
     const auto prob = randint1(100);
     constexpr auto chance_sindarin = 10;
-    char random_artifact_name[1024]{};
     if (prob <= chance_sindarin) {
-        get_table_sindarin(random_artifact_name);
-        return random_artifact_name;
+        return get_table_sindarin();
     }
 
     constexpr auto chance_table = 20;
     if (prob <= chance_table) {
-        get_table_name(random_artifact_name);
-        return random_artifact_name;
+        return get_table_name();
     }
 
     auto filename = get_random_art_filename(armour, power);
+    char random_artifact_name[80]{};
     (void)get_rnd_line(filename.data(), item.artifact_bias, random_artifact_name);
 #ifdef JP
     if (random_artifact_name[0] == 0) {
-        get_table_name(random_artifact_name);
+        return get_table_name();
     }
 #endif
     return random_artifact_name;
index 59a83d1..a6a022e 100644 (file)
@@ -395,12 +395,12 @@ static std::string name_unnatural_random_artifact(PlayerType *player_ptr, ItemEn
     o_ptr->ident |= IDENT_FULL_KNOWN;
     o_ptr->art_name = quark_add("");
     (void)screen_object(player_ptr, o_ptr, 0L);
-    char new_name[1024] = "";
+    char new_name[160] = "";
     if (!get_string(ask_msg, new_name, sizeof new_name) || !new_name[0]) {
         if (one_in_(2)) {
-            get_table_sindarin_aux(new_name);
+            return get_table_sindarin_aux();
         } else {
-            get_table_name_aux(new_name);
+            return get_table_name_aux();
         }
     }
 
index f633264..e618cce 100644 (file)
@@ -92,18 +92,20 @@ static bool object_easy_know(int i)
 
 /*!
  * @brief 各種語彙からランダムな名前を作成する / Create a name from random parts.
- * @param out_string 作成した名を保管する参照ポインタ
+ * @return std::string 作成した名前
  * @details 日本語の場合 aname_j.txt 英語の場合確率に応じて
  * syllables 配列と elvish.txt を組み合わせる。\n
  */
-void get_table_name_aux(char *out_string)
+std::string get_table_name_aux()
 {
+    std::string name;
 #ifdef JP
     char syllable[80];
     get_rnd_line("aname_j.txt", 1, syllable);
-    strcpy(out_string, syllable);
+    name = syllable;
     get_rnd_line("aname_j.txt", 2, syllable);
-    strcat(out_string, syllable);
+    name.append(syllable);
+    return name;
 #else
 #define MAX_SYLLABLES 164 /* Used with scrolls (see below) */
 
@@ -117,70 +119,59 @@ void get_table_name_aux(char *out_string)
         "wed", "werg", "wex", "whon", "wun", "x", "yerg", "yp", "zun", "tri", "blaa", "jah", "bul", "on", "foo", "ju", "xuxu" };
 
     int testcounter = randint1(3) + 1;
-    strcpy(out_string, "");
     if (randint1(3) == 2) {
         while (testcounter--) {
-            strcat(out_string, syllables[randint0(MAX_SYLLABLES)]);
+            name.append(syllables[randint0(MAX_SYLLABLES)]);
         }
     } else {
         char syllable[80];
         testcounter = randint1(2) + 1;
         while (testcounter--) {
             (void)get_rnd_line("elvish.txt", 0, syllable);
-            strcat(out_string, syllable);
+            name.append(syllable);
         }
     }
 
-    out_string[0] = toupper(out_string[1]);
-    out_string[16] = '\0';
+    name[0] = toupper(name[0]);
+    return name;
 #endif
 }
 
 /*!
  * @brief ランダムな名前をアーティファクト銘として整形する。 / Create a name from random parts with quotes.
- * @param out_string 作成した名を保管する参照ポインタ
+ * @return std::string 作成した名前
  * @details get_table_name_aux()ほぼ完全に実装を依存している。
  */
-void get_table_name(char *out_string)
+std::string get_table_name()
 {
-    char buff[80];
-    get_table_name_aux(buff);
-    sprintf(out_string, _("『%s』", "'%s'"), buff);
+    return std::string(_("『", "'")).append(get_table_name_aux()).append(_("』", "'"));
 }
 
 /*!
  * @brief ランダムなシンダリン銘を作成する / Make random Sindarin name
- * @param out_string 作成した名を保管する参照ポインタ
+ * @return std::string 作成した名前
  * @details sname.txtが語幹の辞書となっている。
  */
-void get_table_sindarin_aux(char *out_string)
+std::string get_table_sindarin_aux()
 {
     char syllable[80];
-#ifdef JP
-    char tmp[80];
-#endif
 
     get_rnd_line("sname.txt", 1, syllable);
-    strcpy(_(tmp, out_string), syllable);
+    std::string name = syllable;
     get_rnd_line("sname.txt", 2, syllable);
-#ifdef JP
-    strcat(tmp, syllable);
-    sindarin_to_kana(out_string, tmp);
-#else
-    strcat(out_string, syllable);
-#endif
+    name.append(syllable);
+    return _(sindarin_to_kana(name), name);
 }
 
 /*!
  * @brief シンダリン銘をアーティファクト用に整形する。 / Make random Sindarin name with quotes
  * @param out_string 作成した名を保管する参照ポインタ
+ * @return std::string 作成した名前
  * @details get_table_sindarin_aux()ほぼ完全に実装を依存している。
  */
-void get_table_sindarin(char *out_string)
+std::string get_table_sindarin()
 {
-    char buff[80];
-    get_table_sindarin_aux(buff);
-    sprintf(out_string, _("『%s』", "'%s'"), buff);
+    return std::string(_("『", "'")).append(get_table_sindarin_aux()).append(_("』", "'"));
 }
 
 /*!
index 8dc032c..02e2445 100644 (file)
@@ -2,9 +2,9 @@
 
 #include <string>
 
-void get_table_name_aux(char *out_string);
-void get_table_name(char *out_string);
-void get_table_sindarin_aux(char *out_string);
-void get_table_sindarin(char *out_string);
+std::string get_table_name_aux();
+std::string get_table_name();
+std::string get_table_sindarin_aux();
+std::string get_table_sindarin();
 void flavor_init(void);
 std::string strip_name(short bi_id);
index c18b07a..1fcfc9e 100644 (file)
@@ -40,57 +40,41 @@ static const convert_key s2j_table[] = { { "mb", "nb" }, { "mp", "np" }, { "mv",
 
 /*!
  * @brief シンダリンを日本語の読みに変換する
- * @param kana 変換後の日本語文字列ポインタ
- * @param sindarin 変換前のシンダリン文字列ポインタ
+ * @param sindarin 変換前のシンダリン文字列
+ * @return std::string 変換後のシンダリン文字列
  * @details
  */
-void sindarin_to_kana(char *kana, concptr sindarin)
+std::string sindarin_to_kana(std::string_view sindarin)
 {
-    char buf[256];
-    int idx;
+    std::string kana;
 
-    sprintf(kana, "%s$", sindarin);
-    for (idx = 0; kana[idx]; idx++) {
-        if (isupper(kana[idx])) {
-            kana[idx] = (char)tolower(kana[idx]);
-        }
+    for (const auto &ch : sindarin) {
+        kana.push_back(isupper(ch) ? static_cast<char>(tolower(ch)) : ch);
     }
+    kana.append("$");
 
-    for (idx = 0; s2j_table[idx].key1 != nullptr; idx++) {
+    for (auto idx = 0; s2j_table[idx].key1 != nullptr; idx++) {
         concptr pat1 = s2j_table[idx].key1;
-        concptr pat2 = s2j_table[idx].key2;
-        int len = strlen(pat1);
-        char *src = kana;
-        char *dest = buf;
-
-        while (*src) {
-            if (strncmp(src, pat1, len) == 0) {
-                strcpy(dest, pat2);
-                src += len;
-                dest += strlen(pat2);
+        size_t len = strlen(pat1);
+        std::string::size_type i = 0;
+
+        while (i < kana.length()) {
+            if (strncmp(kana.data() + i, pat1, len) == 0) {
+                concptr pat2 = s2j_table[idx].key2;
+
+                kana.replace(i, len, pat2);
+                i += strlen(pat2);
             } else {
-                if (iskanji(*src)) {
-                    *dest = *src;
-                    src++;
-                    dest++;
+                if (iskanji(kana[i])) {
+                    ++i;
                 }
-                *dest = *src;
-                src++;
-                dest++;
+                ++i;
             }
         }
-
-        *dest = 0;
-        strcpy(kana, buf);
-    }
-
-    idx = 0;
-
-    while (kana[idx] != '$') {
-        idx++;
     }
 
-    kana[idx] = '\0';
+    kana.erase(kana.find('$'));
+    return kana;
 }
 
 /*! 日本語動詞活用 (打つ>打って,打ち etc)
index a86233d..11e3f94 100644 (file)
@@ -3,12 +3,15 @@
 #include "system/angband.h"
 
 #ifdef JP
+#include <string>
+#include <string_view>
+
 constexpr int JVERB_AND = 1;
 constexpr int JVERB_TO = 2;
 constexpr int JVERB_OR = 3;
 void jverb(concptr in, char *out, int flag);
 
-void sindarin_to_kana(char *kana, concptr sindarin);
+std::string sindarin_to_kana(std::string_view sindarin);
 void sjis2euc(char *str);
 void euc2sjis(char *str);
 byte codeconv(char *str);