crscanf
Scanf
This is a Crystal wrapper around the C function scanf, specifically sscanf. It reads numeric and string data types from other strings. Usefully for Crystal users, it returns scanned data as a tuple with specific types, rather than an array of union types.
Installation
Add scanf to your dependencies in your shard.yml file:
dependencies:
scanf:
github: tenebrousedge/crscanf
branch: master
Then run shards install.
Usage
This provides a global scanf macro, which takes a format string as an input, and returns a lambda. The lambda takes a string input, and the return type is dependent on the format string. Some examples:
a = scanf("%3s %d").call("abc 123")
puts a #=> {"abc", 123}
puts typeof(a) #=> Tuple(String, Int32)
lam = scanf("%f -- %c")
b = lam.call("1234 -- q2")
puts b #=> {1234.0, "q"}
puts typeof(b) #=> Tuple(Float32, String)
The format string specification is documented in a number of places, here being one example.
Some specifiers were not implemented. The p specifier does not work, and it probably does not make sense to try to make it work. The j, z, and t length specifiers are not implemented, and they should not be necessary. The L length specifier is not implemented, because Crystal doesn't have Float128. The c specifier is treated exactly the same as s.
Various versions of the C scanf function may be used to read directly from IO objects. This is deliberately not implemented.
Warning
scanf is a dangerous tool. It can easily result in undefined behavior. Test your format strings carefully. This implementation has at least as many bugs as the C version, and likely more.
Development
This shard requires no special libraries, aside from the Crystal compiler. Bugs should be reported via the GitHub issues list.
Contributing
- Fork it (https://github.com/your-github-user/scanf/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Kaia Leahy - creator and maintainer
crscanf
- 0
- 0
- 0
- 0
- 0
- about 8 hours ago
- June 17, 2026
MIT License
Wed, 17 Jun 2026 05:06:01 GMT