OSDN Git Service

[Refactor] #40413 Moved angband_str*() from util.c/h to string-processor.c/h
authorHourier <hourier@users.sourceforge.jp>
Sun, 14 Jun 2020 10:58:57 +0000 (19:58 +0900)
committerHourier <hourier@users.sourceforge.jp>
Sun, 14 Jun 2020 10:58:57 +0000 (19:58 +0900)
52 files changed:
src/autopick/autopick-describer.c
src/autopick/autopick-destroyer.c
src/autopick/autopick-entry.c
src/autopick/autopick-finder.c
src/autopick/autopick-matcher.c
src/autopick/autopick-reader-writer.c
src/cmd-io/cmd-gameoption.c
src/cmd-io/cmd-lore.c
src/cmd-item/cmd-eat.c
src/cmd/cmd-draw.c
src/core/asking-player.c
src/core/scores.c
src/core/show-file.c
src/info-reader/artifact-reader.c
src/info-reader/dungeon-reader.c
src/info-reader/ego-reader.c
src/info-reader/feature-reader.c
src/info-reader/fixed-map-parser.c
src/info-reader/general-parser.c
src/info-reader/kind-reader.c
src/info-reader/magic-reader.c
src/info-reader/race-reader.c
src/info-reader/vault-reader.c
src/inventory/inventory-curse.c
src/inventory/player-inventory.c
src/inventory/recharge-processor.c
src/io/input-key-requester.c
src/io/pref-file-expressor.c
src/knowledge/knowledge-monsters.c
src/locale/english.c
src/locale/japanese.c
src/market/building-monster.c
src/mind/monk-attack.c
src/monster-floor/monster-generator.c
src/monster-race/monster-race-hook.c
src/monster/monster-describer.c
src/mspell/summon-checker.c
src/object/object-flavor.c
src/object/object-hook.c
src/player/player-damage.c
src/player/player-status.c
src/player/process-death.c
src/player/process-name.c
src/spell-kind/spells-detection.c
src/spell/spells-summon.c
src/store/store-util.c
src/term/gameterm.c
src/util/string-processor.c
src/util/string-processor.h
src/util/util.c
src/util/util.h
src/view/display-main-window.c

index cc9f5f2..ae3fd21 100644 (file)
@@ -10,6 +10,7 @@
 #include "autopick/autopick-key-flag-process.h"
 #include "autopick/autopick-methods-table.h"
 #include "system/angband.h"
+#include "util/string-processor.h"
 
 typedef struct
 {
index 601d63c..29c7581 100644 (file)
@@ -20,6 +20,7 @@
 #include "sv-definition/sv-wand-types.h"
 #include "player/player-move.h"
 #include "player/player-race-types.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index cdf25c0..8afa28f 100644 (file)
@@ -15,6 +15,7 @@
 #include "object/object-info.h"
 #include "perception/object-perception.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 
 #ifdef JP
 static char kanji_colon[] = ":";
index 2ec9183..c2c9529 100644 (file)
@@ -17,6 +17,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 
  /*
   * @brief \97^\82¦\82ç\82ê\82½\83A\83C\83e\83\80\82ª\8e©\93®\8fE\82¢\82Ì\83\8a\83X\83g\82É\93o\98^\82³\82ê\82Ä\82¢\82é\82©\82Ç\82¤\82©\82ð\8c\9f\8dõ\82·\82é
index 8a81795..6fa3fb9 100644 (file)
@@ -17,6 +17,7 @@
 #include "object/object-value.h"
 #include "object/object-info.h"
 #include "object-enchant/special-object-flags.h"
+#include "util/string-processor.h"
 
 /*
  * A function for Auto-picker/destroyer
index 70799f3..6dd8e47 100644 (file)
@@ -4,6 +4,7 @@
 #include "io/files-util.h"
 #include "io/read-pref-file.h"
 #include "util/angband-files.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*
index 5307765..51e323e 100644 (file)
@@ -17,6 +17,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 #include "world/world.h"
index 8d66ec3..1d1cbe4 100644 (file)
@@ -11,6 +11,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-lore.h"
 
index 5bbb2d0..d43c25b 100644 (file)
@@ -31,6 +31,7 @@
 #include "spell/spells-status.h"
 #include "sv-definition/sv-food-types.h"
 #include "sv-definition/sv-other-types.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 #include "view/object-describer.h"
index 3dcbdd4..cb0393b 100644 (file)
@@ -11,6 +11,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h" // 暫定。後で消す.
 #include "view/display-messages.h"
 #include "view/display-player.h" // 暫定。後で消す.
index 9a4d1a1..222210c 100644 (file)
@@ -9,6 +9,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*
index 308a0e1..0717d8e 100644 (file)
@@ -34,6 +34,7 @@
 #include "term/term-color-types.h"
 #include "util/angband-files.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 #include "world/world.h"
 
index 08a60dd..93044b1 100644 (file)
@@ -9,6 +9,7 @@
 #include "term/term-color-types.h"
 #include "util/angband-files.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index f576ceb..83dab29 100644 (file)
@@ -3,6 +3,7 @@
 #include "object-enchant/artifact.h"
 #include "object-enchant/tr-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index 1722d77..edabc33 100644 (file)
@@ -5,6 +5,7 @@
 #include "info-reader/parse-error-types.h"
 #include "info-reader/race-info-tokens-table.h"
 #include "io/tokenizer.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index f1c1b2a..7bcf53d 100644 (file)
@@ -3,6 +3,7 @@
 #include "object-enchant/object-ego.h"
 #include "object-enchant/tr-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index 4f4e78e..4fd8dee 100644 (file)
@@ -7,6 +7,7 @@
 #include "room/rooms.h"
 #include "term/gameterm.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 
index 1b5bc2b..8d7d478 100644 (file)
@@ -15,6 +15,7 @@
 #include "io/files-util.h"
 #include "system/system-variables.h"
 #include "util/angband-files.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 #include "world/world.h"
 
index ff90bef..fcab799 100644 (file)
@@ -12,6 +12,7 @@
 #include "system/building-type-definition.h"
 #include "system/system-variables.h"
 #include "util/angband-files.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief パース関数に基づいてデータファイルからデータを読み取る /
index c028c88..4c9130a 100644 (file)
@@ -4,6 +4,7 @@
 #include "object/object-kind.h"
 #include "term/gameterm.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index 5ab22e4..e6baa2d 100644 (file)
@@ -1,4 +1,5 @@
 #include "info-reader/magic-reader.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief 職業魔法情報(m_info)のパース関数 /
index 24a15e6..313b00d 100644 (file)
@@ -1,6 +1,7 @@
 #include "info-reader/race-reader.h"
 #include "info-reader/race-info-tokens-table.h"
 #include "term/gameterm.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index af4ddac..afd0121 100644 (file)
@@ -1,5 +1,6 @@
 #include "info-reader/vault-reader.h"
 #include "room/rooms-vault.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief Vault情報(v_info)のパース関数 /
index 6f8303b..d790f1b 100644 (file)
@@ -20,6 +20,7 @@
 #include "spell-kind/spells-teleport.h"
 #include "spell/spells-summon.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "util/quarks.h"
 #include "view/display-messages.h"
 
index 6e1f811..f87b47f 100644 (file)
@@ -26,6 +26,7 @@
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 
index 34539fc..6d1fcb3 100644 (file)
@@ -5,6 +5,7 @@
 #include "object/object-kind.h"
 #include "player/player-move.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index 92688a8..7bb52f3 100644 (file)
@@ -9,6 +9,7 @@
 #include "term/screen-processor.h" // todo 相互依存している、後で何とかする.
 #include "util/int-char-converter.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h" // todo panel_row_min が依存している。後で移設を検討する.
 #include "view/display-messages.h"
 
index 5b276d6..3d672b6 100644 (file)
@@ -1,6 +1,7 @@
 #include "io/pref-file-expressor.h"
 #include "game-option/runtime-arguments.h"
 #include "system/system-variables.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief process_pref_fileのサブルーチンとして条件分岐処理の解釈と結果を返す
index 6634385..5ee51c1 100644 (file)
@@ -28,6 +28,7 @@
 #include "term/term-color-types.h"
 #include "util/angband-files.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h" // 暫定、後で消す.
 #include "view/display-lore.h"
 #include "view/display-monster-status.h"
index 41e81a6..4281620 100644 (file)
@@ -1,4 +1,5 @@
 #include "locale/english.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief 英単語、句、説を複数形を変換する / Pluralize a monster name
index cc607be..2ef6371 100644 (file)
@@ -5,6 +5,7 @@
  */
 
 #include "locale/japanese.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 #ifdef JP
index 871594c..88bd3c2 100644 (file)
@@ -10,6 +10,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-lore.h"
 
index c8517f2..a7c77e1 100644 (file)
@@ -19,6 +19,7 @@
 #include "monster-race/race-flags1.h"
 #include "monster-race/race-flags3.h"
 #include "monster/monster-status.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 #include "world/world.h"
 
index ea7a7eb..27c2f13 100644 (file)
@@ -22,6 +22,7 @@
 #include "monster/smart-learn-types.h"
 #include "mspell/summon-checker.h"
 #include "spell/spells-summon.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
index 4f14aa0..45e0003 100644 (file)
@@ -17,7 +17,7 @@
 #include "monster/monster-list.h"
 #include "monster/monster-util.h"
 #include "mspell/monster-spell.h"
-#include "util/util.h"
+#include "util/string-processor.h"
 
 /*! 通常pit生成時のモンスターの構成条件ID / Race index for "monster pit (clone)" */
 int vault_aux_race;
index 5356f29..ce08f9c 100644 (file)
@@ -7,6 +7,7 @@
 #include "monster/monster-flag-types.h"
 #include "monster/monster-info.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index e98becc..a947ed9 100644 (file)
@@ -9,6 +9,7 @@
 #include "monster-race/race-indice-types.h"
 #include "monster/monster-util.h"
 #include "spell/spells-summon.h"
+#include "util/string-processor.h"
 
 /*!
  * @brief 指定されたモンスター種族がsummon_specific_typeで指定された召喚条件に合うかどうかを返す
index 103601a..5dcab00 100644 (file)
@@ -36,6 +36,7 @@
 #include "sv-definition/sv-lite-types.h"
 #include "util/bit-flags-calculator.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "world/world.h"
 
  /*!
index cba8474..57fc0ae 100644 (file)
@@ -30,6 +30,7 @@
 #include "sv-definition/sv-protector-types.h"
 #include "sv-definition/sv-weapon-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "world/world.h"
 
index 14833a7..0ccd4ff 100644 (file)
@@ -46,6 +46,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 #include "world/world.h"
index 3bfbb6e..8b2516a 100644 (file)
@@ -75,6 +75,7 @@
 #include "term/screen-processor.h"
 #include "util/bit-flags-calculator.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 #include "view/display-main-window.h"
 #include "view/display-messages.h"
 #include "world/world.h"
index d8a29f7..519c7af 100644 (file)
@@ -19,8 +19,9 @@
 #include "store/store-util.h"
 #include "store/store.h"
 #include "term/gameterm.h"
-#include "util/int-char-converter.h"
 #include "term/screen-processor.h"
+#include "util/int-char-converter.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 #include "world/world.h"
 
index 6c9d7ba..5043af1 100644 (file)
@@ -6,6 +6,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/angband-files.h"
+#include "util/string-processor.h"
 #include "world/world.h"
 
 /*!
index 19d96e9..4356a19 100644 (file)
@@ -16,6 +16,7 @@
 #include "realm/realm-song-numbers.h"
 #include "realm/realm-song.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index e992efc..cf59805 100644 (file)
@@ -25,6 +25,7 @@
 #include "spell/spells-status.h"
 #include "spell/spell-types.h"
 #include "sv-definition/sv-other-types.h"
+#include "util/string-processor.h"
 #include "view/display-messages.h"
 
 /*!
index 1939cb5..258f97d 100644 (file)
@@ -22,6 +22,7 @@
 #include "sv-definition/sv-scroll-types.h"
 #include "sv-definition/sv-weapon-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "world/world-object.h"
 
 int cur_store_num = 0;
index 1d34ce6..baa7b14 100644 (file)
@@ -2,6 +2,7 @@
 #include "system/system-variables.h"
 #include "term/term-color-types.h"
 #include "util/quarks.h"
+#include "util/string-processor.h"
 
  /*
  * Convert an "attr"/"char" pair into a "pict" (P)
index 7f4c372..31c8c77 100644 (file)
@@ -352,3 +352,125 @@ void ascii_to_text(char *buf, concptr str)
 
     *s = '\0';
 }
+
+/*
+ * The angband_strcpy() function copies up to 'bufsize'-1 characters from 'src'
+ * to 'buf' and NUL-terminates the result.  The 'buf' and 'src' strings may
+ * not overlap.
+ *
+ * angband_strcpy() returns strlen(src).  This makes checking for truncation
+ * easy.  Example: if (angband_strcpy(buf, src, sizeof(buf)) >= sizeof(buf)) ...;
+ *
+ * This function should be equivalent to the strlcpy() function in BSD.
+ */
+size_t angband_strcpy(char *buf, concptr src, size_t bufsize)
+{
+#ifdef JP
+    char *d = buf;
+    concptr s = src;
+    size_t len = 0;
+
+    if (bufsize > 0) {
+        /* reserve for NUL termination */
+        bufsize--;
+
+        /* Copy as many bytes as will fit */
+        while (*s && (len < bufsize)) {
+            if (iskanji(*s)) {
+                if (len + 1 >= bufsize || !*(s + 1))
+                    break;
+                *d++ = *s++;
+                *d++ = *s++;
+                len += 2;
+            } else {
+                *d++ = *s++;
+                len++;
+            }
+        }
+        *d = '\0';
+    }
+
+    while (*s++)
+        len++;
+    return len;
+
+#else
+    size_t len = strlen(src);
+    size_t ret = len;
+    if (bufsize == 0)
+        return ret;
+
+    if (len >= bufsize)
+        len = bufsize - 1;
+
+    (void)memcpy(buf, src, len);
+    buf[len] = '\0';
+    return ret;
+#endif
+}
+
+/*
+ * The angband_strcat() tries to append a string to an existing NUL-terminated string.
+ * It never writes more characters into the buffer than indicated by 'bufsize' and
+ * NUL-terminates the buffer.  The 'buf' and 'src' strings may not overlap.
+ *
+ * angband_strcat() returns strlen(buf) + strlen(src).  This makes checking for
+ * truncation easy.  Example:
+ * if (angband_strcat(buf, src, sizeof(buf)) >= sizeof(buf)) ...;
+ *
+ * This function should be equivalent to the strlcat() function in BSD.
+ */
+size_t angband_strcat(char *buf, concptr src, size_t bufsize)
+{
+    size_t dlen = strlen(buf);
+    if (dlen < bufsize - 1) {
+        return (dlen + angband_strcpy(buf + dlen, src, bufsize - dlen));
+    } else {
+        return (dlen + strlen(src));
+    }
+}
+
+/*
+ * A copy of ANSI strstr()
+ *
+ * angband_strstr() can handle Kanji strings correctly.
+ */
+char *angband_strstr(concptr haystack, concptr needle)
+{
+    int l1 = strlen(haystack);
+    int l2 = strlen(needle);
+
+    if (l1 >= l2) {
+        for (int i = 0; i <= l1 - l2; i++) {
+            if (!strncmp(haystack + i, needle, l2))
+                return (char *)haystack + i;
+
+#ifdef JP
+            if (iskanji(*(haystack + i)))
+                i++;
+#endif
+        }
+    }
+
+    return NULL;
+}
+
+/*
+ * A copy of ANSI strchr()
+ *
+ * angband_strchr() can handle Kanji strings correctly.
+ */
+char *angband_strchr(concptr ptr, char ch)
+{
+    for (; *ptr != '\0'; ptr++) {
+        if (*ptr == ch)
+            return (char *)ptr;
+
+#ifdef JP
+        if (iskanji(*ptr))
+            ptr++;
+#endif
+    }
+
+    return NULL;
+}
index 730b6e3..ddc6d85 100644 (file)
@@ -19,3 +19,7 @@ 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);
+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);
+char *angband_strchr(concptr ptr, char ch);
index 7d9f2f1..2b5c3e0 100644 (file)
  */
 errr type_string(concptr str, uint len)
 {
-       errr err = 0;
-       term *old = Term;
-       if (!str) return -1;
-       if (!len) len = strlen(str);
-
-       Term_activate(term_screen);
-       for (concptr s = str; s < str + len; s++)
-       {
-               if (*s == '\0') break;
-
-               err = Term_keypress(*s);
-               if (err) break;
-       }
-
-       Term_activate(old);
-       return err;
+    errr err = 0;
+    term *old = Term;
+    if (!str)
+        return -1;
+    if (!len)
+        len = strlen(str);
+
+    Term_activate(term_screen);
+    for (concptr s = str; s < str + len; s++) {
+        if (*s == '\0')
+            break;
+
+        err = Term_keypress(*s);
+        if (err)
+            break;
+    }
+
+    Term_activate(old);
+    return err;
 }
 
-
 void roff_to_buf(concptr str, int maxlen, char *tbuf, size_t bufsize)
 {
-       int read_pt = 0;
-       int write_pt = 0;
-       int line_len = 0;
-       int word_punct = 0;
-       char ch[3];
-       ch[2] = '\0';
-
-       while (str[read_pt])
-       {
+    int read_pt = 0;
+    int write_pt = 0;
+    int line_len = 0;
+    int word_punct = 0;
+    char ch[3];
+    ch[2] = '\0';
+
+    while (str[read_pt]) {
 #ifdef JP
-               bool kinsoku = FALSE;
-               bool kanji;
+        bool kinsoku = FALSE;
+        bool kanji;
 #endif
-               int ch_len = 1;
-               ch[0] = str[read_pt];
-               ch[1] = '\0';
+        int ch_len = 1;
+        ch[0] = str[read_pt];
+        ch[1] = '\0';
 #ifdef JP
-               kanji = iskanji(ch[0]);
+        kanji = iskanji(ch[0]);
 
-               if (kanji)
-               {
-                       ch[1] = str[read_pt + 1];
-                       ch_len = 2;
+        if (kanji) {
+            ch[1] = str[read_pt + 1];
+            ch_len = 2;
 
-                       if (strcmp(ch, "。") == 0 ||
-                               strcmp(ch, "、") == 0 ||
-                               strcmp(ch, "ィ") == 0 ||
-                               strcmp(ch, "ー") == 0)
-                               kinsoku = TRUE;
-               }
-               else if (!isprint(ch[0]))
-                       ch[0] = ' ';
+            if (strcmp(ch, "。") == 0 || strcmp(ch, "、") == 0 || strcmp(ch, "ィ") == 0 || strcmp(ch, "ー") == 0)
+                kinsoku = TRUE;
+        } else if (!isprint(ch[0]))
+            ch[0] = ' ';
 #else
-               if (!isprint(ch[0]))
-                       ch[0] = ' ';
-#endif
-
-               if (line_len + ch_len > maxlen - 1 || str[read_pt] == '\n')
-               {
-                       int word_len = read_pt - word_punct;
-#ifdef JP
-                       if (kanji && !kinsoku)
-                               /* nothing */;
-                       else
-#endif
-                               if (ch[0] == ' ' || word_len >= line_len / 2)
-                                       read_pt++;
-                               else
-                               {
-                                       read_pt = word_punct;
-                                       if (str[word_punct] == ' ')
-                                               read_pt++;
-                                       write_pt -= word_len;
-                               }
-
-                       tbuf[write_pt++] = '\0';
-                       line_len = 0;
-                       word_punct = read_pt;
-                       continue;
-               }
-
-               if (ch[0] == ' ')
-                       word_punct = read_pt;
-
-#ifdef JP
-               if (!kinsoku) word_punct = read_pt;
-#endif
-
-               if ((size_t)(write_pt + 3) >= bufsize) break;
-
-               tbuf[write_pt++] = ch[0];
-               line_len++;
-               read_pt++;
-#ifdef JP
-               if (kanji)
-               {
-                       tbuf[write_pt++] = ch[1];
-                       line_len++;
-                       read_pt++;
-               }
+        if (!isprint(ch[0]))
+            ch[0] = ' ';
 #endif
-       }
 
-       tbuf[write_pt] = '\0';
-       tbuf[write_pt + 1] = '\0';
-       return;
-}
-
-
-/*
- * The angband_strcpy() function copies up to 'bufsize'-1 characters from 'src'
- * to 'buf' and NUL-terminates the result.  The 'buf' and 'src' strings may
- * not overlap.
- *
- * angband_strcpy() returns strlen(src).  This makes checking for truncation
- * easy.  Example: if (angband_strcpy(buf, src, sizeof(buf)) >= sizeof(buf)) ...;
- *
- * This function should be equivalent to the strlcpy() function in BSD.
- */
-size_t angband_strcpy(char *buf, concptr src, size_t bufsize)
-{
+        if (line_len + ch_len > maxlen - 1 || str[read_pt] == '\n') {
+            int word_len = read_pt - word_punct;
 #ifdef JP
-       char *d = buf;
-       concptr s = src;
-       size_t len = 0;
-
-       if (bufsize > 0) {
-               /* reserve for NUL termination */
-               bufsize--;
-
-               /* Copy as many bytes as will fit */
-               while (*s && (len < bufsize))
-               {
-                       if (iskanji(*s))
-                       {
-                               if (len + 1 >= bufsize || !*(s + 1)) break;
-                               *d++ = *s++;
-                               *d++ = *s++;
-                               len += 2;
-                       }
-                       else
-                       {
-                               *d++ = *s++;
-                               len++;
-                       }
-               }
-               *d = '\0';
-       }
-
-       while (*s++) len++;
-       return len;
-
-#else
-       size_t len = strlen(src);
-       size_t ret = len;
-       if (bufsize == 0) return ret;
-
-       if (len >= bufsize) len = bufsize - 1;
-
-       (void)memcpy(buf, src, len);
-       buf[len] = '\0';
-       return ret;
+            if (kanji && !kinsoku)
+                /* nothing */;
+            else
 #endif
-}
-
-
-/*
- * The angband_strcat() tries to append a string to an existing NUL-terminated string.
- * It never writes more characters into the buffer than indicated by 'bufsize' and
- * NUL-terminates the buffer.  The 'buf' and 'src' strings may not overlap.
- *
- * angband_strcat() returns strlen(buf) + strlen(src).  This makes checking for
- * truncation easy.  Example:
- * if (angband_strcat(buf, src, sizeof(buf)) >= sizeof(buf)) ...;
- *
- * This function should be equivalent to the strlcat() function in BSD.
- */
-size_t angband_strcat(char *buf, concptr src, size_t bufsize)
-{
-       size_t dlen = strlen(buf);
-       if (dlen < bufsize - 1)
-       {
-               return (dlen + angband_strcpy(buf + dlen, src, bufsize - dlen));
-       }
-       else
-       {
-               return (dlen + strlen(src));
-       }
-}
-
-
-/*
- * A copy of ANSI strstr()
- *
- * angband_strstr() can handle Kanji strings correctly.
- */
-char *angband_strstr(concptr haystack, concptr needle)
-{
-       int l1 = strlen(haystack);
-       int l2 = strlen(needle);
-
-       if (l1 >= l2)
-       {
-               for (int i = 0; i <= l1 - l2; i++)
-               {
-                       if (!strncmp(haystack + i, needle, l2))
-                               return (char *)haystack + i;
+                if (ch[0] == ' ' || word_len >= line_len / 2)
+                read_pt++;
+            else {
+                read_pt = word_punct;
+                if (str[word_punct] == ' ')
+                    read_pt++;
+                write_pt -= word_len;
+            }
+
+            tbuf[write_pt++] = '\0';
+            line_len = 0;
+            word_punct = read_pt;
+            continue;
+        }
+
+        if (ch[0] == ' ')
+            word_punct = read_pt;
 
 #ifdef JP
-                       if (iskanji(*(haystack + i))) i++;
+        if (!kinsoku)
+            word_punct = read_pt;
 #endif
-               }
-       }
-
-       return NULL;
-}
 
+        if ((size_t)(write_pt + 3) >= bufsize)
+            break;
 
-/*
- * A copy of ANSI strchr()
- *
- * angband_strchr() can handle Kanji strings correctly.
- */
-char *angband_strchr(concptr ptr, char ch)
-{
-       for (; *ptr != '\0'; ptr++)
-       {
-               if (*ptr == ch) return (char *)ptr;
-
+        tbuf[write_pt++] = ch[0];
+        line_len++;
+        read_pt++;
 #ifdef JP
-               if (iskanji(*ptr)) ptr++;
+        if (kanji) {
+            tbuf[write_pt++] = ch[1];
+            line_len++;
+            read_pt++;
+        }
 #endif
-       }
+    }
 
-       return NULL;
+    tbuf[write_pt] = '\0';
+    tbuf[write_pt + 1] = '\0';
+    return;
 }
 
-
 /*
  * Convert string to lower case
  */
 void str_tolower(char *str)
 {
-       for (; *str; str++)
-       {
+    for (; *str; str++) {
 #ifdef JP
-               if (iskanji(*str))
-               {
-                       str++;
-                       continue;
-               }
+        if (iskanji(*str)) {
+            str++;
+            continue;
+        }
 #endif
-               *str = (char)tolower(*str);
-       }
+        *str = (char)tolower(*str);
+    }
 }
index 52a3a32..ccd855f 100644 (file)
@@ -35,8 +35,4 @@ struct alloc_entry
 
 errr type_string(concptr str, uint len);
 void roff_to_buf(concptr str, int wlen, char *tbuf, size_t bufsize);
-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);
-char *angband_strchr(concptr ptr, char ch);
 void str_tolower(char *str);
index 6bf2248..e1d22a8 100644 (file)
@@ -62,6 +62,7 @@
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "util/bit-flags-calculator.h"
+#include "util/string-processor.h"
 #include "view/display-lore.h"
 #include "view/display-messages.h"
 #include "view/display-player.h"