Converting numbers and booleans to text and back, including Persian digits, with errors for text that is not a number.
import conv
Text arriving from a user, a file or the network is always a string; to do arithmetic with it you have to turn it into a number, and to show the result you turn it back into text. The conv package does both halves of that.
The parsing functions also understand Persian digits (۰۱۲۳۴۵۶۷۸۹) and Arabic ones (٠١٢٣٤٥٦٧٨٩), so a number typed on a Persian keyboard is read without any fuss. Spaces, tabs and line breaks around the text are ignored, and one + or - may come first. Anything else makes the parser throw an Error instead of guessing: an empty string, a stray character such as the abc in 12abc, a sign with no digits, or a number too large for its type. Catch it with try: ... catch e:, or pass it on with try in a function that is itself declared throw.
When a bad value should simply turn into a default, use the Or form of the parser: ParseIntOr, AtoiOr, ParseFloatOr, ParseBoolOr and ParseHexOr take the fallback as their second argument and never throw.
fmt wraps the formatting half of this package with padding and templates; reach for conv when you need the parsing half, or one of the three float formats below. The string methods to_int and to_float are the old lenient readers: they report nothing and give 0 for text that is not a number, so prefer the functions here for anything a user typed.
Things that catch people out:
•
Atoi returns an int, which is 32 bits, so Atoi("3000000000") throws OutOfRange. Use ParseInt for ids, sizes and timestamps.
•
Separators are not digits: 1_000, 1 000 and 1,000 all throw in ParseInt, and so does a decimal point. ParseFloat reads _ between two digits the way a float literal does, so 1_000.5 is fine there.
•
ParseBool is case sensitive: True and YES throw.
•
A comma is not a decimal separator: ParseFloat("1,5") throws. The Persian separator ٫ is fine.
•
The error says where the problem is. e.kind is Syntax or OutOfRange, e.code is one of conv.empty, conv.no_digits, conv.invalid_char, conv.not_bool and conv.overflow, and e.offset, e.line and e.column point at the bad character.
Rejecting bad user input
A form has to say no to 12abc or an empty field, and tell the user why. ParseInt does the checking for you: wrap it in try: and show the message from the error.
conv-guide-strict.salam
import conv
funcmain:
each typed in ["42", " ۱۴۰۳ ", "+7", "12abc", "", "-0015", "99999999999999999999"]:
try:
n := conv.ParseInt(typed)
println"[" + typed + "]", "->", n
catch e:
println"[" + typed + "]", "rejected:", e.message, "at column", e.column
endendend
Output
[42] -> 42
[ ۱۴۰۳ ] -> 1403
[+7] -> 7
[12abc] rejected: invalid character 'a' at column 3
[] rejected: empty input at column 1
[-0015] -> -15
[99999999999999999999] rejected: number does not fit in 64 bits at column 1
Persian digits and the spaces around ۱۴۰۳ are accepted as they are, and leading zeros, as in -0015, are fine too. e.message names the character that broke the number and e.column says where it is, so the user can find it. A number past the 64-bit range is an OutOfRange error rather than a wrapped value.
Adding up numbers in mixed digits
Text pasted from different sources often mixes ASCII, Persian and Arabic digits, and writes the decimal point as . or as the Persian separator ٫. The parsing functions accept all of them, so no clean up is needed before the arithmetic.
conv-guide-sum.salam
import conv
importstrfuncmain throw:
line := "۱۲ 7 ٣٠ 4.5 ۰٫۵"
parts := str.Fields(line)
defer parts.Free()
mut whole := 0asi64mut exact := 0.0each p in parts:
whole += conv.ParseIntOr(p, 0)
exact += try conv.ParseFloat(p)
endprintln"numbers:", parts.Len()
println"sum of whole parts:", whole
println"exact sum:", conv.FormatFloatShort(exact)
end
Output
numbers: 5
sum of whole parts: 49
exact sum: 54
A whole number has no decimal point, so ParseInt rejects 4.5 and ۰٫۵; here ParseIntOr counts them as 0, which is why the two sums differ. Use ParseFloat whenever a value may have a fraction. FormatFloatShort prints the result without a trailing .0 or long tails of digits.
Booleans and hex values from a settings file
Settings files spell their switches in many ways. ParseBool reads true, 1, yes and on as true and false, 0, no and off as false, in lower case, with any spaces around them; anything else throws. Where a misspelled switch should just stay off, ParseBoolOr with a fallback of false does that. ParseHex reads colors, masks and ids written in base sixteen, in either case.
debug: true cache: false compress: false
red: 30 green: 144 blue: 255
limit doubled: 500
accent back to hex: 1e90ff
The prefix 0x is optional for ParseHex (the upper case 0X throw), and so does #, so it is cut first with str.TrimPrefix. The shifts and masks then take the three color channels out of the single number.
Choosing a float format
A decimal number can be turned into text in three ways, and each one suits a different job. FormatFloat writes every digit needed to read back exactly the same number, which is what you want in a file another program will read. FormatFloatShort rounds to six significant digits, like %g in C, which is what a person expects to see. FormatFloatPrec rounds to a fixed number of places, for money and reports.
conv-guide-floats.salam
import conv
funcmain throw:
price := 0.1 * 3.0println"FormatFloat: ", conv.FormatFloat(price)
println"FormatFloatShort:", conv.FormatFloatShort(price)
println"FormatFloatPrec: ", conv.FormatFloatPrec(price, 2)
back := try conv.ParseFloat(conv.FormatFloat(price))
println"round trip exact:", back == price
short := try conv.ParseFloat(conv.FormatFloatShort(price))
println"short is exact: ", short == price
println conv.FormatFloatPrec(1.0 / 3.0, 4), conv.FormatFloatShort(1e21)
end
Output
FormatFloat: 0.30000000000000004
FormatFloatShort: 0.3
FormatFloatPrec: 0.30
round trip exact: true
short is exact: false
0.3333 1e+21
The output shows the trade off: the short form of 0.1 * 3.0 reads as 0.3, but parsing that text gives a slightly different number. Save data with FormatFloat and show it with one of the other two.
Reads the decimal number at the start of the text without reporting any problem: Persian digits, a Persian decimal separator and _ between digits are accepted, "12abc" reads as 12, and text with no number in it gives 0. It is the unchecked reader behind ParseFloat; use ParseFloat when bad input must be an error.
Turns a string into a boolean. "true", "1", "yes" and "on" are true, "false", "0", "no" and "off" are false, with any spaces around them. Anything else throws a Syntax error (code conv.not_bool, or conv.empty for an empty string). The check is case sensitive, so "True" throws.
Reads a whole number, a 64-bit i64. Spaces around it are ignored and one + or - sign may come first; Persian, Arabic and English digits are all understood, even mixed. Throws a Syntax error for an empty string, a sign with no digits or any other character (a decimal point, a separator, a letter), with the offset, line and column of that character, and OutOfRange (code conv.overflow) when the number does not fit in 64 bits.
Reads a decimal number. Scientific notation such as 2.5e3, a leading or trailing point (.5, 5.), inf, infinity and nan in any case, and Persian digits with the Persian decimal separator (۳٫۷۵) are all accepted, with spaces around. As in a float literal, _ may separate two digits (1_000.5, 1e1_0); any other _, as in 1__0, _1 or 1_, is an error. There are no hex floats: 0x1p3 throws. A comma, a second point, an exponent with no digits or any other character throws a Syntax error; a number too large for a 64-bit float throws OutOfRange. A number too small to represent becomes 0.
Returns true when the number is "not a number" (NaN), for instance the result of parsing "nan". No comparison with == can detect this case, because NaN is not even equal to itself.
Rounds to six significant digits and drops trailing zeros, like %g in C: 0.1 + 0.2 comes out as 0.3 and 3.14159265 as 3.14159. From a million up, and below 0.0001, it switches to an exponent, so 1234567.0 is 1.23457e+06.
Turns a decimal number into text with the number of decimal places you ask for, rounding halves away from zero. At most 15 places are written, and a value above about 9e14 falls back to FormatFloat.
Reads a base sixteen number. The 0x prefix is optional and both upper and lower case digits are accepted, but the prefix itself must be a lower case 0x; spaces around are ignored and there is no sign. Up to 16 digits fit; a value above 7fffffffffffffff comes back negative with the same 64 bits, and a longer one throws OutOfRange. Any other character throws a Syntax error.