| 1 | // translate printf message ids before substituting values, as util.h's _() does. |
| 2 | module engine |
| 3 | |
| 4 | import strings |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | fn C.snprintf(&char, uint, &char, ...voidptr) i32 |
| 9 | |
| 10 | type MessageValue = i32 | i64 | int | string | u32 | u64 |
| 11 | |
| 12 | struct MessageArgument { |
| 13 | text string |
| 14 | signed i64 |
| 15 | unsigned u64 |
| 16 | is_string bool |
| 17 | } |
| 18 | |
| 19 | fn message_argument(value MessageValue) MessageArgument { |
| 20 | return match value { |
| 21 | string { MessageArgument{ text: c_string_prefix(value), is_string: true } } |
| 22 | i32 { MessageArgument{ signed: i64(value), unsigned: u64(value) } } |
| 23 | i64 { MessageArgument{ signed: value, unsigned: u64(value) } } |
| 24 | int { MessageArgument{ signed: i64(value), unsigned: u64(value) } } |
| 25 | u32 { MessageArgument{ signed: i64(value), unsigned: u64(value) } } |
| 26 | u64 { MessageArgument{ signed: i64(value), unsigned: value } } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn write_message_argument(output &char, size uint, format string, argument MessageArgument, |
| 31 | conversion u8, wide bool) i32 { |
| 32 | fmt := unsafe { &char(format.str) } |
| 33 | return match conversion { |
| 34 | `s` { C.snprintf(output, size, fmt, argument.text.str) } |
| 35 | `d`, `i` { |
| 36 | if wide { |
| 37 | C.snprintf(output, size, fmt, argument.signed) |
| 38 | } else { |
| 39 | C.snprintf(output, size, fmt, i32(argument.signed)) |
| 40 | } |
| 41 | } |
| 42 | else { |
| 43 | if wide { |
| 44 | C.snprintf(output, size, fmt, argument.unsigned) |
| 45 | } else { |
| 46 | C.snprintf(output, size, fmt, u32(argument.unsigned)) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn format_message_argument(format string, argument MessageArgument, conversion u8, wide bool) ?string { |
| 53 | if (conversion == `s`) != argument.is_string { return none } |
| 54 | length := write_message_argument(unsafe { nil }, 0, format, argument, conversion, wide) |
| 55 | if length < 0 { return none } |
| 56 | buffer := unsafe { &char(C.malloc(uint(length) + 1)) } |
| 57 | if buffer == unsafe { nil } { return none } |
| 58 | defer { C.free(buffer) } |
| 59 | if write_message_argument(buffer, uint(length) + 1, format, argument, conversion, wide) != length { |
| 60 | return none |
| 61 | } |
| 62 | return unsafe { tos_clone(buffer) } |
| 63 | } |
| 64 | |
| 65 | fn render_localized_message(format string, arguments []MessageArgument) ?string { |
| 66 | mut output := strings.new_builder(format.len) |
| 67 | defer { unsafe { output.free() } } |
| 68 | mut position := 0 |
| 69 | mut next_argument := 0 |
| 70 | mut ordering := 0 |
| 71 | for position < format.len { |
| 72 | if format[position] != `%` { |
| 73 | output.write_u8(format[position]) |
| 74 | position++ |
| 75 | continue |
| 76 | } |
| 77 | position++ |
| 78 | if position >= format.len { return none } |
| 79 | if format[position] == `%` { |
| 80 | output.write_u8(`%`) |
| 81 | position++ |
| 82 | continue |
| 83 | } |
| 84 | start := position |
| 85 | mut digits_end := position |
| 86 | for digits_end < format.len && format[digits_end].is_digit() { digits_end++ } |
| 87 | mut argument_index := next_argument |
| 88 | if digits_end > start && digits_end < format.len && format[digits_end] == `$` { |
| 89 | if ordering == 1 { return none } |
| 90 | ordering = 2 |
| 91 | index := format[start..digits_end].parse_uint(10, 32) or { return none } |
| 92 | if index == 0 || index > u64(arguments.len) { return none } |
| 93 | argument_index = int(index) - 1 |
| 94 | position = digits_end + 1 |
| 95 | } else { |
| 96 | if ordering == 2 { return none } |
| 97 | ordering = 1 |
| 98 | next_argument++ |
| 99 | } |
| 100 | if argument_index < 0 || argument_index >= arguments.len { return none } |
| 101 | field_start := position |
| 102 | for position < format.len && format[position] in [`-`, `+`, ` `, `#`, `0`, `'`] { |
| 103 | position++ |
| 104 | } |
| 105 | for position < format.len && format[position].is_digit() { position++ } |
| 106 | if position < format.len && format[position] == `.` { |
| 107 | position++ |
| 108 | for position < format.len && format[position].is_digit() { position++ } |
| 109 | } |
| 110 | length_start := position |
| 111 | if position < format.len && format[position] in [`h`, `l`, `j`, `z`, `t`] { |
| 112 | position++ |
| 113 | if position < format.len && format[position] in [`h`, `l`] |
| 114 | && format[position] == format[length_start] { |
| 115 | position++ |
| 116 | } |
| 117 | } |
| 118 | if position >= format.len { return none } |
| 119 | length := format[length_start..position] |
| 120 | conversion := format[position] |
| 121 | if conversion !in [`s`, `d`, `i`, `u`, `o`, `x`, `X`] { return none } |
| 122 | if conversion == `s` && length != '' { return none } |
| 123 | wide := length in ['l', 'll', 'j', 'z', 't'] |
| 124 | position++ |
| 125 | token := '%' + format[field_start..position] |
| 126 | value := format_message_argument(token, arguments[argument_index], conversion, wide) or { return none } |
| 127 | output.write_string(value) |
| 128 | } |
| 129 | return output.str() |
| 130 | } |
| 131 | |
| 132 | fn localized_message(message string, values ...MessageValue) string { |
| 133 | items := values.map(message_argument(it)) |
| 134 | translation := translated_message(message) |
| 135 | if rendered := render_localized_message(translation, items) { return rendered } |
| 136 | // a malformed catalog must not feed an unchecked format to a variadic call. |
| 137 | return render_localized_message(message, items) or { message } |
| 138 | } |
| 139 | |