Skip to content

Conversation

mbland
Copy link
Owner

@mbland mbland commented Feb 7, 2017

Closes #143 and closes #146. See the commit messages for excruciating details.

Closes #146. Works by adding the ASCII Unit Separator character after
every newline character and using it as the delimiter for `read`. This
way output that shouldn't end with a newline won't.

Seems to add about 0.003-0.005s to the time on average as measured by:

  time COLUMNS=<N> ./go help builtins

regardless of the value of `<N>` used to set `COLUMNS`. Also measurably
faster than the alternate methods of:

- counting the total number of characters printed and only emitting a
  newline if the number of `result` characters hasn't been reached
- counting the number of lines and only emitting a newline if the
  current line number is less than or equal to the length of
  `${result//[^$'\n']}`

Also includes an update to `libexec/get.d/file` to fix a test broken by
this new behavior, as it previously depended upon the old behavior to
add a newline by default.
Noticed the bug when trying to run:

  time COLUMNS=27 ./go help builtins

The script would loop infinitely when trying to print the line for the
`path` builtin. When the `@go.printf` loop would reach the point where
the `prefix` variable contained:

  'script, [alias] or'

the expression:

  line="${line#$prefix}"

would cause `[alias]` to be interpreted as a set of characters to match
per standard Bash pattern matching, rather than as a literal string in
its own right. Consequently, the prefix would not get removed from
`line`, so `line` would remain constant and the loop condition
`"${#line}" -gt "$COLUMNS"` would never fail.

The fix was to use index notation and the length of `prefix` to trim
`line` instead:

  line="${line:${#prefix}}"

The included test case reproduces the bug and verifies its fix.
@mbland mbland added this to the v1.4.0 milestone Feb 7, 2017
@mbland mbland self-assigned this Feb 7, 2017
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.002%) to 95.203% when pulling b010593 on printf into 2e048ee on master.

@mbland mbland merged commit e2bd270 into master Feb 8, 2017
@mbland mbland deleted the printf branch February 8, 2017 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@go.printf shouldn't always add a newline Bump copyright in LICENSE.md
2 participants