#module
#deffunc wordchk val, int
mref txt, 24 : mref ichi, 1
mref stt, 64
peek chk, txt, ichi
if chk = 0 : stt = -1 : return // 範囲外
if (chk > 128 & (chk < 160)) | (chk > 223) : stt = 1 : else : stt = 0 // 1byte=0 2byte=1
return
#deffunc bytechk val, int
mref s, 24 : mref p, 1
mref stt, 64
// 対象コードをチェックする
wordchk s, p
code = stat
if code = -1 : return // 範囲外
// 対象コードより前を確認
repeat , 1
wordchk s, p - cnt
// 全てをチェックし終わったか、1バイト文字が来た
if (stat = -1) || (stat = 0) {
if code = 0 {
// 1バイト領域になったのが奇数回なら半角、偶数回なら全角2バイト目
if cnt \ 2 = 1 : stt = 0 : else : stt = 2
} else {
// 1バイト領域になったのが奇数回なら全角1バイト目、偶数回なら全角2バイト目
if cnt \ 2 = 1 : stt = 1 : else : stt = 2
}
break
}
loop
return
#deffunc strmid2 val, val, int, int
mref result, 24 : mref txt, 25 : mref start, 2 : mref len, 3
if start >= 0 {
// 開始,終了位置を取得
i = 0 // 要素カウンタ
j = 0, 0 // 開始位置, 終了位置
k = start, len // 比較用開始位置, 比較用終了位置
repeat 2
repeat , j
if j.i >= k.i : j.i = cnt : break // 指定位置まで来たらブレイク
wordchk txt, cnt
if stat = -1 : j.i = cnt : break // 範囲外
j.i++
if stat : continue cnt + 2 // 全角文字は2バイト進める
loop
i++
loop
// データ取り出し
strmid result, txt, j.0, j.1 - j.0
} else {
strlen i, txt // 後ろから調べる為にテキストサイズ取得
j = 0 // 文字数カウンタ
repeat i, 1
bytechk txt, i - cnt
if (stat = -1) || j >= len { // 指定位置まで来たらブレイク
k = i - cnt + 1 // 開始位置を決定
break
}
j++
if stat = 2 : continue cnt + 2
loop
strmid result, txt, k, i - k
}
return
#deffunc getstr2 val, val, int, int
mref result, 24 : mref txt, 25 : mref start, 2 : mref code, 3
// 開始位置を取得
i = 0
repeat
wordchk txt, cnt
if stat = -1 : i = cnt : break
i++
if i > start : i = cnt : break
if stat = 1 : continue cnt + 2
loop
// 指定コードまでを抜き出す
getstr result, txt, i, code
return
#global
string = "abあcdeいfうghえijklおmnoかpqきrstくuvwけこxyz"
strmid2 gotten, string, 4, 5
mes "元のテキスト " + string
mes "4文字目から5文字 " + gotten
strmid2 gotten, string, -1, 7
mes "後ろから7文字 " + gotten
mes
string = "あいう\\うえお\\かきく\\けこ"
getstr2 gotten , string, 9, '\\'
mes "元のテキスト " + string
mes "9文字目以降の'\\'まで " + gotten
stop
|