std.text
Unicode の細かい扱いと正規表現。 upper() や split() のような日常的な操作は string のメソッドにある。
import std.text;
print(text.width("さめ")); // 4
text.normalize text.width text.fold_case text.code text.from_code text.is_digit text.is_alpha text.is_space text.is_upper text.is_lower text.compare text.compare_ja text.sort_ja text.regex text.decode text.encode Regex.find Regex.find_all Regex.matches Regex.replace Regex.split Match.text Match.start Match.end Match.group
関数
- text.normalize(s: string, form: string) → string¶
-
Unicode の正規化。
s string 直す文字列 form string "NFC"(合成)か "NFD"(分解) print(text.normalize("が", "NFD").len()); // 2(か と 濁点) print(text.normalize("が", "NFC").len()); // 1
- text.width(s: string) → int¶
-
表示幅。全角は 2 と数える。等幅で桁を揃えるときに使う。
print(text.width("さめ")); // 4 print(text.width("shark")); // 5
- text.fold_case(s: string) → string¶
-
大文字小文字を無視して比べるための形にする。
print(text.fold_case("Shark") == text.fold_case("SHARK")); // true
- text.code(c: string) → int¶
-
1文字をコードポイントの番号にする。
print(text.code("A")); // 65
- text.from_code(n: int) → string?¶
-
コードポイントの番号から1文字を作る。番号がおかしければ none。
print(text.from_code(65) ?? ""); // A
- text.is_digit(c: string) → bool¶
-
数字かどうか。
print(text.is_digit("7")); // true print(text.is_digit("さ")); // false
- text.is_alpha(c: string) → bool¶
-
文字(数字や記号ではないもの)かどうか。
print(text.is_alpha("さ")); // true
- text.is_space(c: string) → bool¶
-
空白かどうか。改行やタブも含む。
print(text.is_space(" ")); // true
- text.is_upper(c: string) → bool¶
-
大文字かどうか。
print(text.is_upper("A")); // true
- text.is_lower(c: string) → bool¶
-
小文字かどうか。
print(text.is_lower("a")); // true
- text.compare(a: string, b: string) → int¶
-
コードポイント順に比べる。小さいと -1、同じなら 0、大きいと 1。
print(text.compare("あ", "い")); // -1
- text.compare_ja(a: string, b: string) → int¶
-
五十音順に比べる。ひらがなとカタカナは同じものとして扱う。
print(text.compare_ja("さめ", "あじ")); // 1
- text.sort_ja(ref xs: list<string>) → void¶
-
五十音順に並べ替える。漢字は対象外で、コードポイント順に置かれる。
var xs = ["サメ", "あじ", "ぶり", "いわし"]; text.sort_ja(ref xs); print(xs.join(", ")); // あじ, いわし, サメ, ぶり
- text.regex(pattern: string) → Result<Regex>¶
-
正規表現を組み立てる。書けないパターンなら失敗を返す。
使えるのは文字クラス、* + ? { }、|、( )、^ $ まで。先読みや後方参照は持たない。
if var re = text.regex("[0-9]+") { print(re.matches("さめ2匹")); // true }
- text.decode(b: bytes, enc: string) → Result<string>¶
-
バイト列を文字列にする。
b bytes 元のバイト列 enc string 文字コード。"utf-8" は必ず使える。ほかは処理系による if var s = text.decode("さめ".bytes(), "utf-8") { print(s); } // さめ
- text.encode(s: string, enc: string) → Result<bytes>¶
-
文字列をバイト列にする。
if var b = text.encode("さめ", "utf-8") { print(b.len()); } // 6
Regex
組み立てた正規表現。
- Regex.find(s: string) → Match?¶
-
最初に見つかった1つ。無ければ none。
if var re = text.regex("([0-9]{4})-([0-9]{2})-([0-9]{2})") { if var m = re.find("期限は 2026-08-15 です") { print(m.text()); // 2026-08-15 } }
- Regex.find_all(s: string) → list<Match>¶
-
見つかったものを全部。
if var re = text.regex("[0-9]+") { print(re.find_all("さめ2匹、ふぐ3匹").len()); // 2 }
- Regex.matches(s: string) → bool¶
-
見つかるかどうか。
if var re = text.regex("^さめ") { print(re.matches("さめ2匹")); // true }
- Regex.replace(s: string, to: string) → string¶
-
見つかったところを置き換える。$1 $2 で ( ) の中を差し込める。
if var re = text.regex("([0-9]{4})-([0-9]{2})-([0-9]{2})") { print(re.replace("2026-08-15", "$1年$2月$3日")); // 2026年08月15日 }
- Regex.split(s: string) → list<string>¶
-
見つかったところで区切る。
if var re = text.regex("[,、]") { print(re.split("さめ,ふぐ、あじ").len()); // 3 }
Match
正規表現で見つかった1か所。
- Match.text() → string¶
-
一致した部分の文字列。
if var re = text.regex("[0-9]+") { if var m = re.find("さめ2匹") { print(m.text()); } // 2 }
- Match.start() → int¶
-
始まりの位置(文字単位、0 から)。
if var re = text.regex("[0-9]+") { if var m = re.find("さめ2匹") { print(m.start()); } // 2 }
- Match.end() → int¶
-
終わりの位置。ここは含まない。
if var re = text.regex("[0-9]+") { if var m = re.find("さめ2匹") { print(m.end()); } // 3 }
- Match.group(i: int) → string?¶
-
( ) で囲んだ部分。1 から数える。無ければ none。
if var re = text.regex("([0-9]{4})-([0-9]{2})") { if var m = re.find("2026-08") { print(m.group(1) ?? ""); } // 2026 }