[ruby-dev:48443] [ruby-trunk - Bug #10078] [Closed] String#count() returns wrong count under specific conditions

From: nobu@...
Date: 2014-07-22 03:56:36 UTC
List: ruby-dev #48443
Issue #10078 has been updated by Nobuyoshi Nakada.

Status changed from Open to Closed
% Done changed from 0 to 100

Applied in changeset r46896.

----------
string.c: fix wrong single-byte optimization

* string.c (rb_str_count): fix wrong single-byte optimization.
  7bit ascii can be a trailing byte in Shift_JIS.
  [ruby-dev:48442] [Bug #10078]

----------------------------------------
Bug #10078: String#count() returns wrong count under specific conditions
https://bugs.ruby-lang.org/issues/10078#change-47952

* Author: Hiroki Najima
* Status: Closed
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: ruby 2.0.0p481 (2014-05-08) [i386-mingw32]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
以下の条件の場合、String#countが誤った文字数を返します。

* 文字コードがWindows-31J (文字コード体系が違うせいか、UTF_8, ISO2022_JP, EUC_JPでは再現せず)
* countの引数が1バイト文字一つのみ
* Stringにcountに指定された文字と一致する文字コードが含まれる。

異常な例: 和は0x9861, aは0x61

~~~
>ruby -ve "puts 'a-和'.count('a')"
ruby 2.0.0p481 (2014-05-08) [i386-mingw32]
2
~~~

正常な例: countに2バイト以上与えると動作が正しくなります。
'a-a'などtr由来のcountの動作も的確ですが、'a-a'と'a'は等価なはずなのに異常な例と結果が異なります。

~~~
>ruby -ve "puts 'a-和'.count('a-a')"
ruby 2.0.0p481 (2014-05-08) [i386-mingw32]
1

>ruby -ve "puts 'a-和'.count('ab')"
ruby 2.0.0p481 (2014-05-08) [i386-mingw32]
1
~~~




-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next