| 1 | module scheme |
| 2 | |
| 3 | const unicode_native_descriptors = [ |
| 4 | NativeDescriptor{'char-titlecase', unicode_char_titlecase}, |
| 5 | NativeDescriptor{'char-foldcase', unicode_char_foldcase}, |
| 6 | NativeDescriptor{'char-title-case?', unicode_char_title_case}, |
| 7 | NativeDescriptor{'char-general-category', unicode_char_general_category}, |
| 8 | NativeDescriptor{'digit-value', unicode_digit_value}, |
| 9 | NativeDescriptor{'string-upcase', unicode_string_upcase}, |
| 10 | NativeDescriptor{'string-downcase', unicode_string_downcase}, |
| 11 | NativeDescriptor{'string-titlecase', unicode_string_titlecase}, |
| 12 | NativeDescriptor{'string-foldcase', unicode_string_foldcase}, |
| 13 | NativeDescriptor{'string-normalize-nfd', unicode_string_nfd}, |
| 14 | NativeDescriptor{'string-normalize-nfkd', unicode_string_nfkd}, |
| 15 | NativeDescriptor{'string-normalize-nfc', unicode_string_nfc}, |
| 16 | NativeDescriptor{'string-normalize-nfkc', unicode_string_nfkc}, |
| 17 | ]! |
| 18 | |
| 19 | const unicode_category_names = ['Cn', 'Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Mn', 'Mc', 'Me', 'Nd', 'Nl', |
| 20 | 'No', 'Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po', 'Sm', 'Sc', 'Sk', 'So', 'Zs', 'Zl', 'Zp', 'Cc', |
| 21 | 'Cf', 'Cs', 'Co']! |
| 22 | |
| 23 | fn (mut vm VM) install_unicode_primitives() { |
| 24 | for index in 0 .. unicode_native_descriptors.len { |
| 25 | entry := &unicode_native_descriptors[index] |
| 26 | vm.bind_global(entry.name, procedure_value(&Procedure{ mode: .native, name: entry.name, owner: vm.owner, native: entry.callback })) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn unicode_char_titlecase(mut _vm VM, args []Value) !Value { |
| 31 | check_arity('char-titlecase', args.len, 1, 1)! |
| 32 | return character(unicode_simple_case(args[0].as_character()!, 2)) |
| 33 | } |
| 34 | |
| 35 | fn unicode_char_foldcase(mut _vm VM, args []Value) !Value { |
| 36 | check_arity('char-foldcase', args.len, 1, 1)! |
| 37 | return character(unicode_simple_fold(args[0].as_character()!)) |
| 38 | } |
| 39 | |
| 40 | fn unicode_char_title_case(mut _vm VM, args []Value) !Value { |
| 41 | check_arity('char-title-case?', args.len, 1, 1)! |
| 42 | return boolean(C.vile_unicode17_category(i32(args[0].as_character()!)) == 3) |
| 43 | } |
| 44 | |
| 45 | fn unicode_char_general_category(mut _vm VM, args []Value) !Value { |
| 46 | check_arity('char-general-category', args.len, 1, 1)! |
| 47 | category := C.vile_unicode17_category(i32(args[0].as_character()!)) |
| 48 | // Category spelling is case-sensitive data, independent of identifier folding. |
| 49 | return text_value(.symbol, unicode_category_names[category]) |
| 50 | } |
| 51 | |
| 52 | fn unicode_digit_value(mut _vm VM, args []Value) !Value { |
| 53 | check_arity('digit-value', args.len, 1, 1)! |
| 54 | digit := unicode_decimal(args[0].as_character()!) |
| 55 | return if digit < 0 { boolean(false) } else { integer(digit) } |
| 56 | } |
| 57 | |
| 58 | fn unicode_string_upcase(mut _vm VM, args []Value) !Value { |
| 59 | return unicode_transform_args(args, 'string-upcase', .upper) |
| 60 | } |
| 61 | |
| 62 | fn unicode_string_downcase(mut _vm VM, args []Value) !Value { |
| 63 | return unicode_transform_args(args, 'string-downcase', .lower) |
| 64 | } |
| 65 | |
| 66 | fn unicode_string_titlecase(mut _vm VM, args []Value) !Value { |
| 67 | return unicode_transform_args(args, 'string-titlecase', .title) |
| 68 | } |
| 69 | |
| 70 | fn unicode_string_foldcase(mut _vm VM, args []Value) !Value { |
| 71 | return unicode_transform_args(args, 'string-foldcase', .fold) |
| 72 | } |
| 73 | |
| 74 | fn unicode_string_nfd(mut _vm VM, args []Value) !Value { |
| 75 | return unicode_transform_args(args, 'string-normalize-nfd', .nfd) |
| 76 | } |
| 77 | |
| 78 | fn unicode_string_nfkd(mut _vm VM, args []Value) !Value { |
| 79 | return unicode_transform_args(args, 'string-normalize-nfkd', .nfkd) |
| 80 | } |
| 81 | |
| 82 | fn unicode_string_nfc(mut _vm VM, args []Value) !Value { |
| 83 | return unicode_transform_args(args, 'string-normalize-nfc', .nfc) |
| 84 | } |
| 85 | |
| 86 | fn unicode_string_nfkc(mut _vm VM, args []Value) !Value { |
| 87 | return unicode_transform_args(args, 'string-normalize-nfkc', .nfkc) |
| 88 | } |
| 89 | |