[Ttssh2-commit] [3991] strreplace に正規表現を指定できるようにした。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 8月 14日 (土) 01:20:09 JST


Revision: 3991
          http://sourceforge.jp/projects/ttssh2/svn/view?view=rev&revision=3991
Author:   yutakapon
Date:     2010-08-14 01:20:09 +0900 (Sat, 14 Aug 2010)

Log Message:
-----------
strreplace に正規表現を指定できるようにした。

Modified Paths:
--------------
    trunk/doc/en/html/macro/command/strreplace.html
    trunk/doc/ja/html/macro/command/strreplace.html
    trunk/teraterm/ttpmacro/ttl.c


-------------- next part --------------
Modified: trunk/doc/en/html/macro/command/strreplace.html
===================================================================
--- trunk/doc/en/html/macro/command/strreplace.html	2010-08-12 15:52:42 UTC (rev 3990)
+++ trunk/doc/en/html/macro/command/strreplace.html	2010-08-13 16:20:09 UTC (rev 3991)
@@ -20,19 +20,31 @@
 <h2>Format</h2>
 
 <pre class="macro-syntax">
-strreplace &lt;strvar&gt; &lt;index&gt; &lt;oldstr&gt; &lt;newstr&gt;
+strreplace &lt;strvar&gt; &lt;index&gt; &lt;regex&gt; &lt;newstr&gt;
 </pre>
 
 <h2>Remarks</h2>
 
 <p>
-Replaces a specified &lt;oldstr&gt; string in the string variable &lt;strvar&gt; beginning at a specified position &lt;index&gt;(1-origin), with another specified &lt;newstr&gt; string.<br>
-If the &lt;newstr&gt; is a null character(''), the &lt;oldstr&gt; string is removed.
+Replaces a specified &lt;regex&gt; string in the string variable &lt;strvar&gt; beginning at a specified position &lt;index&gt;(1-origin), with another specified &lt;newstr&gt; string.<br>
+If the &lt;newstr&gt; is a null character(''), the &lt;regex&gt; string is removed.
 </p>
 
 <h2>Example</h2>
 
 <pre class="macro-example">
+src='linux.txt'
+strreplace src 1 '\.txt' '.doc'
+messagebox src 'result'          ; linux.doc
+</pre>
+
+<pre class="macro-example">
+src='I can do it.'
+strreplace src 1 'can\s' ''
+messagebox src 'result'     ; 'I do it.'
+</pre>
+
+<pre class="macro-example">
 src='Microsoft Windows XP [Version 5.1.2600]'
 strmatch src '(Version \d+.\d+.)\d+'
 
@@ -47,12 +59,6 @@
 endif
 </pre>
 
-<pre class="macro-example">
-src='I can do it.'
-strreplace src 1 'can' ''
-messagebox src 'result'     ; 'I  do it.'
-</pre>
-
 <h2>Reference</h2>
 
 <a href="strmatch.html">strmatch</a><br>

Modified: trunk/doc/ja/html/macro/command/strreplace.html
===================================================================
--- trunk/doc/ja/html/macro/command/strreplace.html	2010-08-12 15:52:42 UTC (rev 3990)
+++ trunk/doc/ja/html/macro/command/strreplace.html	2010-08-13 16:20:09 UTC (rev 3991)
@@ -20,19 +20,31 @@
 <h2>Œ`Ž®</h2>
 
 <pre class="macro-syntax">
-strreplace &lt;strvar&gt; &lt;index&gt; &lt;oldstr&gt; &lt;newstr&gt;
+strreplace &lt;strvar&gt; &lt;index&gt; &lt;regex&gt; &lt;newstr&gt;
 </pre>
 
 <h2>‰ðà</h2>
 
 <p>
-•¶Žš—ñ•Ï” &lt;strvar&gt; ‚Ì &lt;index&gt; ˆÊ’ui1ƒIƒŠƒWƒ“j‚©‚çA•¶Žš—ñ &lt;oldstr&gt; ‚ðŒŸõ‚µA•¶Žš—ñ &lt;newstr&gt; ‚É’u‚«Š·‚¦‚éB<br>
-•¶Žš—ñ &lt;newstr&gt; ‚ª‹ó('')‚̏ꍇA•¶Žš—ñ &lt;oldstr&gt; ‚͍폜‚³‚ê‚éB
+•¶Žš—ñ•Ï” &lt;strvar&gt; ‚Ì &lt;index&gt; ˆÊ’ui1ƒIƒŠƒWƒ“j‚©‚çA³‹K•\Œ» &lt;regex&gt; ‚ðŒŸõ‚µA•¶Žš—ñ &lt;newstr&gt; ‚É’u‚«Š·‚¦‚éB<br>
+•¶Žš—ñ &lt;newstr&gt; ‚ª‹ó('')‚̏ꍇA³‹K•\Œ» &lt;regex&gt; ‚͍폜‚³‚ê‚éB
 </p>
 
 <h2>—á</h2>
 
 <pre class="macro-example">
+src='linux.txt'
+strreplace src 1 '\.txt' '.doc'
+messagebox src 'result'          ; linux.doc
+</pre>
+
+<pre class="macro-example">
+src='I can do it.'
+strreplace src 1 'can\s' ''
+messagebox src 'result'     ; 'I do it.'
+</pre>
+
+<pre class="macro-example">
 src='Microsoft Windows XP [Version 5.1.2600]'
 strmatch src '(Version \d+.\d+.)\d+'
 
@@ -47,12 +59,6 @@
 endif
 </pre>
 
-<pre class="macro-example">
-src='I can do it.'
-strreplace src 1 'can' ''
-messagebox src 'result'     ; 'I  do it.'
-</pre>
-
 <h2>ŽQÆ</h2>
 
 <a href="strmatch.html">strmatch</a><br>

Modified: trunk/teraterm/ttpmacro/ttl.c
===================================================================
--- trunk/teraterm/ttpmacro/ttl.c	2010-08-12 15:52:42 UTC (rev 3990)
+++ trunk/teraterm/ttpmacro/ttl.c	2010-08-13 16:20:09 UTC (rev 3991)
@@ -3481,13 +3481,13 @@
 
 WORD TTLStrReplace()
 {
-	WORD Err, VarId;
+	WORD Err, VarId, VarType;
 	TStrVal oldstr;
 	TStrVal newstr;
-	char *srcptr;
+	char *srcptr, *matchptr;
 	char *p;
-	int srclen, oldlen;
-	int pos;
+	int srclen, oldlen, matchlen;
+	int pos, ret;
 	int result = 0;
 
 	memset(oldstr, 0, MaxStrLen);
@@ -3510,7 +3510,10 @@
 		goto error;
 	}
 
+	oldlen = strlen(oldstr);
+
 	// strptr•¶Žš—ñ‚Ì pos •¶Žš–ڈȍ~‚É‚¨‚¢‚āAoldstr ‚ð’T‚·B
+#if 0
 	p = strstr(srcptr + (pos - 1), oldstr);
 	if (p == NULL) {
 		// Œ©‚‚©‚ç‚È‚©‚Á‚½ê‡‚́A"0"‚Å–ß‚éB
@@ -3519,12 +3522,37 @@
 	}
 
 	// ‚Ü‚¸‚Í oldstr ‚ðíœ‚·‚é
-	oldlen = strlen(oldstr);
 	remove_string(srcptr, p - srcptr + 1, oldlen);
 
 	// newstr ‚ð‘}“ü‚·‚é
 	insert_string(srcptr, p - srcptr + 1, newstr);
+#else
+	p = srcptr + (pos - 1);
+	ret = FindRegexStringOne(oldstr, oldlen, p, strlen(p));
+	// FindRegexStringOne‚Ì’†‚ÅUnlockVar()‚³‚ê‚Ä‚µ‚Ü‚¤‚̂ŁALockVar()‚µ‚È‚¨‚·B
+	LockVar();
+	if (ret == 0) {
+		// Œ©‚‚©‚ç‚È‚©‚Á‚½ê‡‚́A"0"‚Å–ß‚éB
+		result = 0;
+		goto error;
+	}
 
+	if (CheckVar("matchstr",&VarType,&VarId) &&
+		(VarType==TypString)) {
+		matchptr = StrVarPtr(VarId);
+		matchlen = strlen(matchptr);
+	} else {
+		result = 0;
+		goto error;
+	}
+
+	// ‚Ü‚¸‚Í oldstr ‚ðíœ‚·‚é
+	remove_string(srcptr, (pos - 1) + ret, matchlen);
+
+	// newstr ‚ð‘}“ü‚·‚é
+	insert_string(srcptr, (pos - 1) + ret, newstr);
+#endif
+
 	result = 1;
 
 error:



Ttssh2-commit メーリングリストの案内
Back to archive index