LoginSignup
5
4

More than 5 years have passed since last update.

文字列の長さを取得するときはlengthを使ってはいけない

Last updated at Posted at 2016-12-23

自分の進捗表示用パッケージprogressにPRが来た。

Bars: bug on narrow terminals when non-ascii fill character is used

どうやら文字列の長さを得るときはlengthではいけない時があるそうだ。

どういうことか

import std.stdio;

void main()
{
    string ascii = "a";
    string non_ascii = "あ";
    writeln(ascii.length);
    writeln(non_ascii.length);
}
出力結果
1
3

"a""あ"も1文字のはずだが、"あ".length3になってしまっている。

writeln(cast(void[]) ascii);
writeln(cast(void[]) non_ascii);
出力結果
[97]
[227, 129, 130]

lengthは文字列のバイト数を返しているようだ。

std.range.primitives.walkLength

std.range.primitives.walkLengthは、empty == trueになるまで何回popFront()ができるか実際に試してみて、それで得られた長さを返す。

import std.stdio;
import std.range : walkLength;

void main()
{
    string ascii = "a";
    string non_ascii = "あ";
    writeln(ascii.walkLength);
    writeln(non_ascii.walkLength);
}
出力結果
1
1

ちゃんと正しい文字数が得られている。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4