# StringScanner#scan BUG?

**URL:** <https://www.ruby-forum.com/t/stringscanner-scan-bug/75397>\
**Category:** Development\
**Created:** [November 9, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/stringscanner-scan-bug/75397 "2006-11-09T17:41:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/u/SSSBSHS_SSSSS5SRSmS)\
**Post date:** [November 9, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/stringscanner-scan-bug/75397/1 "2006-11-09T17:41:24Z")

</div>

e$B$H$\_$?$G$9!#e(B

Ruby 1.8.5 e$B$G\<!$N%3!\<%I$r\<B9T$9$k$H!"e(B$KCODE=“NONE”  
e$B$+$D@55,I=8=$N%U%ie(B  
e$B%0$,e(B e, s, u e$B$N\>l9g$K!“e(B"a:b” e$B$rJV$7$^$9!#e(B

require “strscan”  
str = “a:b”  
[“NONE”, “EUC”, “SJIS”, “UTF8”].each do |code|  
p code  
$KCODE = code  
p StringScanner.new(str).scan(/[^\x01:]+/n)  
p StringScanner.new(str).scan(/[^\x01:]+/e)  
p StringScanner.new(str).scan(/[^\x01:]+/s)  
p StringScanner.new(str).scan(/[^\x01:]+/u)  
end

[^\x01:] e$B$re(B [^:\x01] e$B$K$9$k$HH/@8$7$^$;$s!#4|BT$I$\*$je(B “a”  
e$B$rJV$7$^$9!#e(B

e$B$O$8$a$Oe(B Ruby  
e$BK\BN$N@55,I=8=$N%P%0$+$H;W$C$?$N$G$9$,!"e(BStringScanner e$B0Je(B  
e$B30$G$OH/@8$7$J$$$h$&$G$9!#e(B

---

<div class="post-metadata">

**Author:** ![SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/u/SSSBSHS_SSSSS5SRSmS)\
**Post date:** [January 7, 2007, 2:34am UTC](https://www.ruby-forum.com/t/stringscanner-scan-bug/75397/2 "2007-01-07T02:34:41Z")

</div>

e$B@DLZ$G$9!#$9$\_$^$;$s!"$9$C$+$j8+Mn$H$7$F$^$7$?!#e(B

In mail “[ruby-dev:29914] StringScanner#scan BUG?”  
e$B$H$\_$?$^$5$R$me(B [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> p StringScanner.new(str).scan(/[^\x01:]+/n)  
> p StringScanner.new(str).scan(/[^\x01:]+/e)  
> p StringScanner.new(str).scan(/[^\x01:]+/s)  
> p StringScanner.new(str).scan(/[^\x01:]+/u)  
> end
> 
> [^\x01:] e$B$re(B [^:\x01] e$B$K$9$k$HH/@8$7$^$;$s!#4|BT$I$\*$je(B “a” e$B$rJV$7$^$9!#e(B
> 
> e$B$O$8$a$Oe(B Ruby e$BK\BN$N@55,I=8=$N%P%0$+$H;W$C$?$N$G$9$,!"e(BStringScanner e$B0Je(B  
> e$B30$G$OH/@8$7$J$$$h$&$G$9!#e(B

strscan.c e$B$Ge(B kcode\_set\_option  
e$B$r8F$s$G$J$+$C$?$N$,860x$G$7$?!#e(B

e$B0J2\<$N%Q%C%A$G=$@5$G$-$k$N$G$9$,!"e(B1.8  
e$B%V%i%s%A$K%3%\_%C%H$7$F$h$$$G$7$g$&$+!)e(B  
extern e$B4X?t$,e(B 2 e$B$DA}$($^$9!#e(B

# Index: intern.h

# RCS file: /var/cvs/src/ruby/intern.h,v retrieving revision 1.139.2.21 diff -u -r1.139.2.21 intern.h — intern.h 30 Oct 2006 02:24:08 -0000 1.139.2.21 +++ intern.h 7 Jan 2007 01:33:27 -0000 @@ -370,6 +370,8 @@ int rb\_reg\_options \_((VALUE)); void rb\_set\_kcode \_((const char\*)); const char\* rb\_get\_kcode \_((void)); +void rb\_kcode\_set\_option \_((VALUE)); +void rb\_kcode\_reset\_option \_((void)); /\* ruby.c \*/ RUBY\_EXTERN VALUE rb\_argv; RUBY\_EXTERN VALUE rb\_argv0; Index: re.c

RCS file: /var/cvs/src/ruby/re.c,v  
retrieving revision 1.114.2.20  
diff -u -r1.114.2.20 re.c  
— re.c 20 Jul 2006 07:04:17 -0000 1.114.2.20  
+++ re.c 7 Jan 2007 01:31:23 -0000  
@@ -200,8 +200,8 @@

static int curr\_kcode;

-static void  
-kcode\_set\_option(re)  
+void  
+rb\_kcode\_set\_option(re)  
VALUE re;  
{  
if (!FL\_TEST(re, KCODE\_FIXED)) return;  
@@ -224,8 +224,8 @@  
}  
}

-static void  
-kcode\_reset\_option()  
+void  
+rb\_kcode\_reset\_option()  
{  
if (reg\_kcode == curr\_kcode) return;  
switch (reg\_kcode) {  
@@ -253,9 +253,9 @@

```
 if (!FL_TEST(re, KCODE_FIXED))

```

return mbclen(c);

- kcode\_set\_option(re);

- rb\_kcode\_set\_option(re);  
len = mbclen(c);

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();  
return len;  
}

@@ -486,11 +486,11 @@  
}  
if (\*ptr == ‘:’ && ptr[len-1] == ‘)’) {  
Regexp \*rp;

- 

```
 kcode_set_option(re);

```

- 

```
 rb_kcode_set_option(re);
 rp = ALLOC(Regexp);
 MEMZERO((char *)rp, Regexp, 1);
 err = re_compile_pattern(++ptr, len -= 2, rp) != 0;

```

- 

```
 kcode_reset_option();

```

- 

```
 rb_kcode_reset_option();
 re_free_pattern(rp);

```

}  
if (err) {  
@@ -849,7 +849,7 @@  
char \*err;

if (FL\_TEST(re, KCODE\_FIXED))

- 

```
 kcode_set_option(re);

```

- 

```
 rb_kcode_set_option(re);

```

rb\_reg\_check(re);  
RREGEXP(re)-\>ptr-\>fastmap\_accurate = 0;  
err = re\_compile\_pattern(RREGEXP(re)-\>str, RREGEXP(re)-\>len,  
RREGEXP(re)-\>ptr);  
@@ -870,9 +870,9 @@  
if (may\_need\_recompile) rb\_reg\_prepare\_re(re);

if (FL\_TEST(re, KCODE\_FIXED))

- kcode\_set\_option(re);

- rb\_kcode\_set\_option(re);  
else if (reg\_kcode != curr\_kcode)

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();

- kcode\_set\_option(re);

- rb\_kcode\_set\_option(re);  
else if (reg\_kcode != curr\_kcode)

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();

- kcode\_set\_option((VALUE)re);

- rb\_kcode\_set\_option((VALUE)re);  
}  
if (ruby\_ignorecase) {  
options |= RE\_OPTION\_IGNORECASE;  
@@ -1376,7 +1376,7 @@  
re-\>str[len] = ‘\0’;  
re-\>len = len;  
if (options & ~0xf) {

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();  
}  
if (ruby\_in\_compile) FL\_SET(obj, REG\_LITERAL);  
}  
@@ -1839,7 +1839,7 @@  
}  
StringValue(str);  
str = rb\_reg\_quote(str);

- kcode\_reset\_option();

- rb\_kcode\_reset\_option();  
return str;  
}

# Index: ext/strscan/strscan.c

RCS file: /var/cvs/src/ruby/ext/strscan/strscan.c,v  
retrieving revision 1.7.2.8  
diff -u -r1.7.2.8 strscan.c  
— ext/strscan/strscan.c 26 Jul 2006 09:37:00 -0000 1.7.2.8  
+++ ext/strscan/strscan.c 7 Jan 2007 01:31:23 -0000  
@@ -403,6 +403,7 @@  
if (S\_RESTLEN(p) \< 0) {  
return Qnil;  
}

- rb\_kcode\_set\_option(regex);  
if (headonly) {  
ret = re\_match(RREGEXP(regex)-\>ptr,  
CURPTR(p), S\_RESTLEN(p),  
@@ -416,6 +417,7 @@  
S\_RESTLEN(p),  
&(p-\>regs));  
}

- rb\_kcode\_reset\_option();

---

<div class="post-metadata">

**Author:** ![SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@SSSBSHS\_SSSSS5SRSmS](https://www.ruby-forum.com/u/SSSBSHS_SSSSS5SRSmS)\
**Post date:** [January 7, 2007, 11:00am UTC](https://www.ruby-forum.com/t/stringscanner-scan-bug/75397/3 "2007-01-07T11:00:17Z")

</div>

ã€€å•é¡Œã‚ã‚Šã¾ã›ã‚“ã€‚ãŠé¡˜ã„ã—ã¾ã™ã€‚

At Sun, 7 Jan 2007 10:34:15 +0900,  
Minero A. wrote:

> > [^\x01:] ã‚’ [^:\x01] ã«ã™ã‚‹ã¨ç™ºç”Ÿã—ã¾ã›ã‚“ã€‚æœŸå¾…ã©ãŠã‚Š “a” ã‚’è¿”ã—ã¾ã™ã€‚
> > 
> > ã¯ã˜ã‚ã¯ Ruby æœ¬ä½“ã®æ­£è¦è¡¨ç¾ã®ãƒã‚°ã‹ã¨æ€ã£ãŸã®ã§ã™ãŒã€StringScanner ä»¥  
> > å¤–ã§ã¯ç™ºç”Ÿã—ãªã„ã‚ˆã†ã§ã™ã€‚
> 
> strscan.c ã§ kcode\_set\_option ã‚’å‘¼ã‚“ã§ãªã‹ã£ãŸã®ãŒåŽŸå› ã§ã—ãŸã€‚
> 
> ä»¥ä¸‹ã®ãƒ‘ãƒƒãƒã§ä¿®æ­£ã§ãã‚‹ã®ã§ã™ãŒã€1.8 ãƒ–ãƒ©ãƒ³ãƒã«ã‚³ãƒŸãƒƒãƒˆã—ã¦ã‚ˆã„ã§ã—ã‚‡ã†ã‹ï¼Ÿ  
> extern é–¢æ•°ãŒ 2 ã¤å¢—ãˆã¾ã™ã€‚

–  
/  
/\_\_ \_\_ [Akinori.org](http://Akinori.org) / [MUSHA.org](http://MUSHA.org)  
/ ) ) ) ) / [FreeBSD.org](http://FreeBSD.org) / [Ruby-lang.org](http://Ruby-lang.org)  
Akinori MUSHA aka / (\_ / ( (\_\_( @ [iDaemons.org](http://iDaemons.org) / and.or.jp

“Different eyes see different things,  
Different hearts beat on different strings –  
But there are times for you and me when all such things agree”
