文字列の掛け算(繰り返し、リフレイン)を追加するパッチ

作ってみたけど、これはなかなか便利だよ。まあ、繰り返す数が自動的に文字列に変換されてまた数値に戻してってやってるのはなんだか無駄っぽく感じてしまうけど。

--- hspvar_str.cpp.orig	Mon Jan 14 09:30:26 2008
+++ hspvar_str.cpp	Sat Feb 16 10:50:19 2008
@@ -194,6 +194,35 @@
 	myproc->aftertype = HSPVAR_FLAG_STR;
 }
 
+// Mul
+static void HspVarStr_MulI( PDAT *pval, const void *val )
+{
+	char **pp;
+	int n;
+	pp = (char **)sbGetOption( (char *)pval );
+	n = atoi( (char *)val );
+	if( n <= 0 ) {
+		sbStrCopy( pp, "" );
+	} else if( n >= 2 ) {
+		char *p;
+		STRINF *st;
+		int sz, newsize;
+		int i;
+		st = sbGetSTRINF( *pp );
+		p = st->ptr;
+		sz = (int)strlen( p );
+		newsize = sz * n + 1;
+		newsize = ( newsize + 0xfff ) & 0xfffff000;						// 8K単位で確保
+		p = sbExpand( *pp, newsize );
+		*pp = p;
+		for( i = 1; i < n; i++ ) {
+			memcpy( p+sz*i, p, sz );
+		}
+		p[sz * n] = '\0';
+	}
+	myproc->aftertype = HSPVAR_FLAG_STR;
+}
+
 // Eq
 static void HspVarStr_EqI( PDAT *pdat, const void *val )
 {
@@ -263,7 +292,7 @@
 
 	p->AddI = HspVarStr_AddI;
 //	p->SubI = HspVarStr_Invalid;
-//	p->MulI = HspVarStr_Invalid;
+	p->MulI = HspVarStr_MulI;
 //	p->DivI = HspVarStr_Invalid;
 //	p->ModI = HspVarStr_Invalid;
 
OpenHSP
Copyright (C) 1997-2008, Onion Software/onitama, all rights reserved.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.

以下のような感じで使えます。

repeat 10
	mes "hello" * cnt
loop
mes strlen( "hello" * 10000 )

これを実行すると以下のように出ます。

hello
hellohello
hellohellohello
hellohellohellohello
hellohellohellohellohello
hellohellohellohellohellohello
hellohellohellohellohellohellohello
hellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohello
50000