OSDN Git Service

[Feature] 文字列操作ユーティリティ関数
authorHabu <habu1010+github@gmail.com>
Fri, 9 Apr 2021 14:05:37 +0000 (23:05 +0900)
committerHabu <habu1010+github@gmail.com>
Sat, 10 Apr 2021 09:26:41 +0000 (18:26 +0900)
- str_trim: 文字列の両端の空白を削除する
- str_rtrim: 文字列の右端の空白を削除する
- str_ltrim: 文字列の左端の空白を削除する
- str_split: 文字列を指定文字で分割する
- str_erase: 文字列から指定した文字を削除する

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

index 20c2bae..bb03696 100644 (file)
@@ -537,3 +537,129 @@ int strrncmp(const char *s1, const char *s2, int len)
 
     return (0);
 }
+
+/**
+ * @brief 文字列の両端の空白を削除する
+ *
+ * 文字列 str の両端にある空白(スペースおよびタブ)を削除し、
+ * 削除した文字列を std::string 型のオブジェクトとして返す。
+ * 文字列全体が空白の場合は空文字列を返す。
+ *
+ * @param str 操作の対象とする文字列
+ * @return std::string strの両端の空白を削除した文字列
+ */
+std::string str_trim(std::string_view str)
+{
+    const auto start_pos = str.find_first_not_of(" \t");
+    const auto end_pos = str.find_last_not_of(" \t");
+
+    if (start_pos == std::string_view::npos || end_pos == std::string_view::npos) {
+        return std::string();
+    }
+
+    return std::string(str.substr(start_pos, end_pos - start_pos + 1));
+}
+
+/**
+ * @brief 文字列の右端の空白を削除する
+ *
+ * 文字列 str の右端にある空白(スペースおよびタブ)を削除し、
+ * 削除した文字列を std::string 型のオブジェクトとして返す。
+ * 文字列全体が空白の場合は空文字列を返す。
+ *
+ * @param str 操作の対象とする文字列
+ * @return std::string strの右端の空白を削除した文字列
+ */
+std::string str_rtrim(std::string_view str)
+{
+    const auto end_pos = str.find_last_not_of(" \t");
+
+    if (end_pos == std::string_view::npos) {
+        return std::string();
+    }
+
+    return std::string(str.substr(0, end_pos + 1));
+}
+
+/**
+ * @brief 文字列の左端の空白を削除する
+ *
+ * 文字列 str の左端にある空白(スペースおよびタブ)を削除し、
+ * 削除した文字列を std::string 型のオブジェクトとして返す。
+ * 文字列全体が空白の場合は空文字列を返す。
+ *
+ * @param str 操作の対象とする文字列
+ * @return std::string strの左端の空白を削除した文字列
+ */
+std::string str_ltrim(std::string_view str)
+{
+    const auto start_pos = str.find_first_not_of(" \t");
+
+    if (start_pos == std::string_view::npos) {
+        return std::string();
+    }
+
+    return std::string(str.substr(start_pos));
+}
+
+/**
+ * @brief 文字列を指定した文字で分割する
+ *
+ * 文字列 str を delim で指定した文字で分割し、分割した文字列を要素とする配列を
+ * std::vector<std::string> 型のオブジェクトとして返す。
+ *
+ * @param str 操作の対象とする文字列
+ * @param delim 文字列を分割する文字
+ * @return std::vector<std::string> 分割した文字列を要素とする配列
+ */
+std::vector<std::string> str_split(std::string_view str, char delim)
+{
+    std::vector<std::string> result;
+
+    while (true) {
+        bool found = false;
+        for (size_t i = 0; i < str.size(); ++i) {
+            if (str[i] == delim) {
+                result.emplace_back(str.substr(0, i));
+                str.remove_prefix(i + 1);
+                found = true;
+                break;
+            }
+#ifdef JP
+            if (iskanji(str[i]))
+                ++i;
+#endif
+        }
+        if (!found) {
+            result.emplace_back(std::move(str));
+            return result;
+        }
+    }
+}
+
+/**
+ * @brief 文字列から指定した文字を取り除く
+ *
+ * 文字列 str から文字列 erase_chars に含まれる文字をすべて削除し、
+ * 削除した文字列を std::string 型のオブジェクトとして返す。
+ *
+ * @param str 操作の対象とする文字列
+ * @param erase_chars 削除する文字を指定する文字列
+ * @return std::string 指定した文字をすべて削除した文字列
+ */
+std::string str_erase(std::string str, std::string_view erase_chars)
+{
+    for (auto it = str.begin(); it != str.end();) {
+        if (erase_chars.find(*it) != std::string_view::npos) {
+            it = str.erase(it);
+            continue;
+        }
+#ifdef JP
+        if (iskanji(*it))
+            ++it;
+#endif
+        ++it;
+    }
+
+    return str;
+}
index 74bf815..151b446 100644 (file)
@@ -2,6 +2,10 @@
 
 #include "system/angband.h"
 
+#include <string>
+#include <string_view>
+#include <vector>
+
 #define MAX_MACRO_MOD 12
 #define MAX_MACRO_TRIG 200 /*!< 登録を許すマクロ(トリガー)の最大数 */
 
@@ -26,3 +30,8 @@ char *angband_strchr(concptr ptr, char ch);
 char *ltrim(char *p);
 char *rtrim(char *p);
 int strrncmp(const char *s1, const char *s2, int len);
+std::string str_trim(std::string_view str);
+std::string str_rtrim(std::string_view str);
+std::string str_ltrim(std::string_view str);
+std::vector<std::string> str_split(std::string_view str, char delim);
+std::string str_erase(std::string str, std::string_view erase_chars);