FILE名:mod_word.as
文字列をbyte単位ではなく文字数単位で処理するモジュールです
#module mod_dstr
#const	SPRIT_MAX			99999
#const  SPRIT_CUT_LENGTH	1024
#const	FALSE				0
#const	TRUE				1
;-----------------------------------------------------
#define global dstr_split(%1 = "", %2 = "", %3 = "", %4 = SPRIT_MAX@mod_dstr, %5 = SPRIT_CUT_LENGTH@mod_dstr) _dstr_split %1, %2, %3, %4, %5
;-----------------------------------------------------
#defcfunc getByte_u8 int p1
	if ((p1 >= 0x00) && (p1 <= 0x7f)) {
		return 1
	} else : if ((p1 >= 0xc2) && (p1 <= 0xdf)) {
		return 2
	} else : if ((p1 >= 0xe0) && (p1 <= 0xef)) {
		return 3
	} else : if ((p1 >= 0xf0) && (p1 <= 0xf7)) {
		return 4
	} else : if ((p1 >= 0xf8) && (p1 <= 0xfb)) {
		return 5		// 廃止された?
	} else : if ((p1 >= 0xfc) && (p1 <= 0xfd)) {
		return 6		// 廃止された?
	}
return 0
;-----------------------------------------------------
; dstr_len
;
; 文字列の長さを調べる(バイト単位ではなく、文字数で調べます)
;
;
; buff         : 文字列の長さを調べたい文字列型の変数名
;
;
;=====================================================
; SAMPLE
;
;	moji = "AIUえお"
;   c = dstr_len(moji)
;   pos 0, 0 : picmes c   ;結果:5
;
;-----------------------------------------------------
#defcfunc dstr_len str buff
	sdim in_buff, strlen(buff)
	c_cd			= 0
	count_index		= 0
	set_index		= 0
	in_buff			= buff
	len_buff		= strlen(in_buff)
	repeat len_buff
		if set_index >= len_buff : break
		c_cd = peek(in_buff, set_index)
		plus_index = getByte_u8(c_cd)
		count_index++
		set_index += plus_index
	loop
	in_buff = ""
	sdim in_buff, 1
	return count_index
;-----------------------------------------------------
; dstr_instr
;
; 文字列の探索(バイト単位ではなく、文字数で調べます)
;
;
; str z_buff         探索元の文字列
; int ind            index
; str srarch_buff    探索する文字列
;
;=====================================================
; SAMPLE
;
;	moji = "AIUえお"
;   c = dstr_len(moji)
;   pos 0, 0 : picmes c   ;結果:5
;
;-----------------------------------------------------
#defcfunc dstr_instr str z_buff, int ind, str serach_buff
	r = -1
	c_buff = ""
	s = dstr_len(serach_buff)
	i = dstr_len(z_buff) - s + 1
	sdim in_buff, strlen(z_buff)
	g_length	= s
	c_cd		= 0
	in_buff		= z_buff
	len_buff	= strlen(in_buff)
	repeat i
		s_index = cnt + ind
		count_index	= 0
		set_index	= 0
		plus_index	= 0
		index_flg	= FALSE
		get_index	= 0
		sdim re_buff, 3, g_length
		repeat len_buff
			if set_index >= len_buff : break
			c_cd = peek(in_buff, set_index)
			plus_index = getByte_u8(c_cd)
			if cnt >= s_index {
				if get_index < g_length {
					index_flg = TRUE
				} else {
					break
				}
				get_index++
			}
			if index_flg {
				re_buff(count_index) = strmid(in_buff, set_index, plus_index)
				count_index++
			}
			set_index += plus_index
		loop
		join_buff	= ""
		loop_length	= g_length
		if loop_length > count_index : loop_length = count_index
		repeat loop_length
			join_buff += re_buff(cnt)
		loop
		if join_buff == serach_buff {
			r = cnt
			break
		}
	loop
	in_buff = ""
	sdim in_buff, 1
	sdim re_buff, 1, 1
	return r
;-----------------------------------------------------
; dstr_mid
;
; 文字列の一部を取り出す(バイト単位ではなく、文字数で取り出します)
;
;
; buff         : 取り出すもとの文字列が格納されている変数名
; start_index  : 取り出し始めのインデックス(開始位置は0から)
; get_length   : 取り出す文字数
;
;
;=====================================================
; SAMPLE
;
;	moji_A = "AIUえお"
;   moji_B = dstr_mid(moji_A, 3, 2)
;   pos 0, 0 : picmes moji_B   ;結果"えお"
;
;-----------------------------------------------------
#defcfunc dstr_mid str buff, int start_index, int get_length
	sdim in_buff, strlen(buff)
	c_cd		= 0
	count_index	= 0
	set_index	= 0
	plus_index	= 0
	index_flg	= FALSE
	get_index	= 0
	in_buff		= buff
	len_buff	= strlen(in_buff)
	if start_index == -1 {
		sdim re_buff, 3, len_buff
	} else {
		sdim re_buff, 3, get_length
	}
	repeat len_buff
		if set_index >= len_buff : break
		c_cd = peek(in_buff, set_index)
		
		plus_index = getByte_u8(c_cd)
		if start_index == -1 {
			index_flg = TRUE
		} else {
			if cnt >= start_index {
				if get_index < get_length {
					index_flg = TRUE
				} else {
					break
				}
				get_index++
			}
		}
		if index_flg {
			re_buff(count_index) = strmid(in_buff, set_index, plus_index)
			count_index++
		}
		set_index += plus_index
	loop
	join_buff	= ""
	loop_length	= get_length
	if loop_length > count_index : loop_length = count_index
	switch start_index
	case -1
		repeat loop_length
			c			= count_index - loop_length + cnt
			join_buff	+= re_buff(c)
		loop
		swbreak
	default
		repeat loop_length
			join_buff += re_buff(cnt)
		loop
		swbreak
	swend
	in_buff = ""
	sdim in_buff, 1
	sdim re_buff, 1, 1
	return join_buff
#global
超簡単TEST
#include "hsp3utf.as"
#include "mod_word.as"
// 簡単なTEST
#if 1
mes dstr_len("abcアイウ123")
mes dstr_len("+-*高低αβ")
a = "あいうえおかきくけこ"
b = dstr_instr(a, 0, "きく")
mes b
mes str(b)
mes dstr_mid(a, 0, 5)
mes str(dstr_len(a))
#endif
(11). 配布等について
当モジュールはフリー(商用可)です。
使う人がいるか不明ですが、もし使われる場合は配布用ReadMe等に当モジュールを使用している旨を記載する必要はありません。
もし使われる際は、メール(simakuroneko@gmail.com)もしくは、
掲示板(
http://simakuroneko.bbs.fc2.com/)に使用するとだけ一言書いてもらえると作者が喜びます。
再配布もOKですが、その場合事後で結構ですのでE-Mail( simakuroneko@gmail.com )までご連絡ください。
また、書籍等に掲載する場合も自由に掲載していただいてOKです。
その場合も事後で結構ですのでE-Mail( simakuroneko@gmail.com )までご連絡頂けると嬉しいです。
・改変に関して
お好きなように改変していただいて結構です。
むしろもっと使いやすくしていただいて配布していただけると嬉しいです。(私も楽したいので)
by しまくろねこさん
#ということらしいです
#codetterに記載してある命令を流用しました
#
https://codetter.com/?p=1571
#getByte_u8 の返り値が0になるケースもあったため