Skip to content

Commit

Permalink
Improve documentation of std.string.column.
Browse files Browse the repository at this point in the history
  • Loading branch information
H. S. Teoh committed Sep 19, 2014
1 parent 84d2b52 commit 2c47808
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions std/string.d
Expand Up @@ -4406,8 +4406,13 @@ string[string] abbrev(string[] values) @safe pure


/******************************************
* Compute column number after string if string starts in the
* leftmost column, which is numbered starting from 0.
* Compute _column number at the end of the printed form of the string,
* assuming the string starts in the leftmost _column, which is numbered
* starting from 0.
*
* Tab characters are expanded into enough spaces to bring the _column number
* to the next multiple of tabsize, and carriage returns and newlines reset the
* running _column number back to 0.
*/

size_t column(S)(S str, in size_t tabsize = 8) @safe pure @nogc if (isSomeString!S)
Expand Down Expand Up @@ -4437,6 +4442,30 @@ size_t column(S)(S str, in size_t tabsize = 8) @safe pure @nogc if (isSomeString
return column;
}

///
unittest
{
assert(column("1234 ") == 5);

// Tab stops are set at 8 spaces by default; tab characters insert enough
// spaces to bring the column position to the next multiple of 8.
assert(column("\t") == 8);
assert(column("1\t") == 8);
assert(column("\t1") == 9);
assert(column("123\t") == 8);

// Other tab widths are possible by specifying it explicitly:
assert(column("\t", 4) == 4);
assert(column("1\t", 4) == 4);
assert(column("\t1", 4) == 5);
assert(column("123\t", 4) == 4);

// Newlines and carriage returns reset the column number.
assert(column("abc\n") == 0);
assert(column("abc\n1") == 1);
assert(column("abcdefg\r1234") == 4);
}

@safe @nogc unittest
{
debug(string) printf("string.column.unittest\n");
Expand Down

0 comments on commit 2c47808

Please sign in to comment.