onionhammer / nim-templates

A simple string templating library for Nim
BSD 3-Clause "New" or "Revised" License
93 stars 9 forks source link

Fix issue with single character disappearing when at the start of a template #9

Closed olafal0 closed 5 years ago

olafal0 commented 5 years ago

Hello! I recently ran into a minor issue, and thought I'd try my hand at fixing it. Here's an example that fails:

import templates
proc example(x = 5): string = tmpli js"-$(x)"
assert example() == js"-5"

The output is 5, instead of -5 as expected. This seems to be caused by the behavior of trim_eol when it reaches the first character of its input. In the above example, trim_eol will be called on the string "-", immediately reach the first character, and return an empty string, since it hasn't encountered any non-whitespace. The fix is just to reorder so that we check for non-whitespace first, before returning an empty string. I added a regression test for this as well.

I also made a commit to remove the deprecated stmt and expr metatypes, and replace them with typed and untyped instead.

Let me know if you have any suggestions, I'm new to the language so criticism is welcome :smile: Thanks!

onionhammer commented 5 years ago

Looks good, thanks for the contribution!