Skip to content

Conversation

@kobi2187
Copy link
Contributor

Add several Python Format Specification Mini-Language features:

  • Sign-aware padding (= alignment): places padding after sign/prefix but before digits, e.g. "{-42:0=10}" -> "-000000042"

  • Thousands separators (, and _): group digits with separators

    • Comma (,) for decimal: "{1234567:,}" -> "1,234,567"
    • Underscore (_) for all bases: "{255:_b}" -> "1111_1111"
    • Binary/octal/hex use groups of 4, decimal uses groups of 3
  • Percentage type (%): multiplies by 100 and adds % suffix "{0.25:.2%}" -> "25.00%"

  • Character type (c): converts integer to Unicode character "{65:c}" -> "A"

Zero padding with grouping properly integrates zeros into the grouped digits for both integers and floats, matching Python behavior.

Added comprehensive test suite based on Python's format specification.

Add several Python Format Specification Mini-Language features:

- Sign-aware padding (= alignment): places padding after sign/prefix
  but before digits, e.g. "{-42:0=10}" -> "-000000042"

- Thousands separators (, and _): group digits with separators
  - Comma (,) for decimal: "{1234567:,}" -> "1,234,567"
  - Underscore (_) for all bases: "{255:_b}" -> "1111_1111"
  - Binary/octal/hex use groups of 4, decimal uses groups of 3

- Percentage type (%): multiplies by 100 and adds % suffix
  "{0.25:.2%}" -> "25.00%"

- Character type (c): converts integer to Unicode character
  "{65:c}" -> "A"

Zero padding with grouping properly integrates zeros into the grouped
digits for both integers and floats, matching Python behavior.

Added comprehensive test suite based on Python's format specification.
@Araq
Copy link
Member

Araq commented Dec 17, 2025

Tell your AI model to use more helper procs, less return, optimize string slices to direct character copy loops and in general follow the existing style. And even after all that is done it looks like pure technical debt, nobody asked for "even more python like f-strings". Already this one-char-based mini-language is undecipherable nonsense, Nim allows for real DSLs, Nim is not Python.

@kobi2187
Copy link
Contributor Author

kobi2187 commented Dec 17, 2025

thought you'd be happy we make the stdlib more complete. There's a lot of python code out there, when one wants to port to Nim, it can save a lot of trouble if it has higher compatibility. and afaik, the string format came from python, but i could be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants