Punycode (RFC3492) in OCaml

fix(lint): use Fmt instead of Printf/Format (E205)

Replace Printf.sprintf/printf and Format.asprintf/fprintf with
Fmt.str/pr/pf across publicsuffix, punycode, rate-limit, requests,
and openamp packages. Add fmt dependency to gen_corpus executables.

+29 -38
+1 -1
fuzz/dune
··· 6 6 (executable 7 7 (name gen_corpus) 8 8 (modules gen_corpus) 9 - (libraries unix)) 9 + (libraries unix fmt)) 10 10 11 11 (rule 12 12 (alias runtest)
+1 -1
fuzz/gen_corpus.ml
··· 14 14 write "seed_003" (String.make 16 '\x00'); 15 15 write "seed_004" (String.make 16 '\xff'); 16 16 write "seed_005" (String.init 256 Char.chr); 17 - Printf.printf "gen_corpus: wrote 6 seed files\n" 17 + Fmt.pr "gen_corpus: wrote 6 seed files\n"
+18 -23
test/test_punycode.ml
··· 30 30 let result = Punycode.encode input in 31 31 check string "encode" expected result 32 32 with Punycode.Error e -> 33 - fail (Format.asprintf "encode failed: %a" Punycode.pp_error_reason e) 33 + fail (Fmt.str "encode failed: %a" Punycode.pp_error_reason e) 34 34 35 35 let check_decode_ok expected input = 36 36 try ··· 39 39 check int "length" (Array.length expected_arr) (Array.length result); 40 40 Array.iteri 41 41 (fun i u -> 42 - check int 43 - (Printf.sprintf "char %d" i) 42 + check int (Fmt.str "char %d" i) 44 43 (Uchar.to_int expected_arr.(i)) 45 44 (Uchar.to_int u)) 46 45 result 47 46 with Punycode.Error e -> 48 - fail (Format.asprintf "decode failed: %a" Punycode.pp_error_reason e) 47 + fail (Fmt.str "decode failed: %a" Punycode.pp_error_reason e) 49 48 50 49 let check_utf8_roundtrip s = 51 50 try ··· 53 52 let decoded = Punycode.decode_utf8 encoded in 54 53 check string "roundtrip" s decoded 55 54 with Punycode.Error e -> 56 - fail (Format.asprintf "roundtrip failed: %a" Punycode.pp_error_reason e) 55 + fail (Fmt.str "roundtrip failed: %a" Punycode.pp_error_reason e) 57 56 58 57 (* RFC 3492 Section 7.1 Test Vectors *) 59 58 ··· 609 608 let result = Punycode.encode_label "example" in 610 609 check string "ascii passthrough" "example" result 611 610 with Punycode.Error e -> 612 - fail (Format.asprintf "encode_label failed: %a" Punycode.pp_error_reason e) 611 + fail (Fmt.str "encode_label failed: %a" Punycode.pp_error_reason e) 613 612 614 613 let test_label_encode_german () = 615 614 try 616 615 let result = Punycode.encode_label "münchen" in 617 616 check string "german label" "xn--mnchen-3ya" result 618 617 with Punycode.Error e -> 619 - fail (Format.asprintf "encode_label failed: %a" Punycode.pp_error_reason e) 618 + fail (Fmt.str "encode_label failed: %a" Punycode.pp_error_reason e) 620 619 621 620 let test_label_decode_german () = 622 621 try 623 622 let result = Punycode.decode_label "xn--mnchen-3ya" in 624 623 check string "german decode" "münchen" result 625 624 with Punycode.Error e -> 626 - fail (Format.asprintf "decode_label failed: %a" Punycode.pp_error_reason e) 625 + fail (Fmt.str "decode_label failed: %a" Punycode.pp_error_reason e) 627 626 628 627 (* IDNA tests *) 629 628 let test_idna_to_ascii_simple () = ··· 631 630 let result = Punycode_idna.to_ascii "münchen.example.com" in 632 631 check string "idna to_ascii" "xn--mnchen-3ya.example.com" result 633 632 with Punycode_idna.Error e -> 634 - fail (Format.asprintf "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 633 + fail (Fmt.str "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 635 634 636 635 let test_idna_to_unicode_simple () = 637 636 try 638 637 let result = Punycode_idna.to_unicode "xn--mnchen-3ya.example.com" in 639 638 check string "idna to_unicode" "münchen.example.com" result 640 639 with Punycode_idna.Error e -> 641 - fail 642 - (Format.asprintf "to_unicode failed: %a" Punycode_idna.pp_error_reason e) 640 + fail (Fmt.str "to_unicode failed: %a" Punycode_idna.pp_error_reason e) 643 641 644 642 let test_idna_roundtrip () = 645 643 let original = "münchen.example.com" in ··· 648 646 let unicode = Punycode_idna.to_unicode ascii in 649 647 check string "idna roundtrip" original unicode 650 648 with Punycode_idna.Error e -> 651 - fail 652 - (Format.asprintf "roundtrip failed: %a" Punycode_idna.pp_error_reason e) 649 + fail (Fmt.str "roundtrip failed: %a" Punycode_idna.pp_error_reason e) 653 650 654 651 let test_idna_all_ascii () = 655 652 try 656 653 let result = Punycode_idna.to_ascii "www.example.com" in 657 654 check string "all ascii passthrough" "www.example.com" result 658 655 with Punycode_idna.Error e -> 659 - fail (Format.asprintf "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 656 + fail (Fmt.str "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 660 657 661 658 let test_idna_mixed_labels () = 662 659 try ··· 667 664 (String.length result > 12 668 665 && String.sub result (String.length result - 12) 12 = ".example.com") 669 666 with Punycode_idna.Error e -> 670 - fail (Format.asprintf "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 667 + fail (Fmt.str "to_ascii failed: %a" Punycode_idna.pp_error_reason e) 671 668 672 669 (* Case annotation tests *) 673 670 let test_case_annotation_decode () = ··· 686 683 (* a should be lowercase *) 687 684 check bool "a lowercase" true (case_flags.(1) = Punycode.Lowercase) 688 685 with Punycode.Error e -> 689 - fail 690 - (Format.asprintf "decode_with_case failed: %a" Punycode.pp_error_reason e) 686 + fail (Fmt.str "decode_with_case failed: %a" Punycode.pp_error_reason e) 691 687 692 688 let test_case_annotation_encode () = 693 689 let codepoints = codepoints_of_hex_list [ 0x0061; 0x0062; 0x0063 ] in ··· 700 696 (* Should encode as "AbC-" (basic code points with case annotation) *) 701 697 check string "case encoded" "AbC-" result 702 698 with Punycode.Error e -> 703 - fail 704 - (Format.asprintf "encode_with_case failed: %a" Punycode.pp_error_reason e) 699 + fail (Fmt.str "encode_with_case failed: %a" Punycode.pp_error_reason e) 705 700 706 701 (* Edge case tests *) 707 702 let test_empty_input () = ··· 722 717 let result = Punycode.encode input in 723 718 check string "pure ascii" "hello-" result 724 719 with Punycode.Error e -> 725 - fail (Format.asprintf "encode failed: %a" Punycode.pp_error_reason e) 720 + fail (Fmt.str "encode failed: %a" Punycode.pp_error_reason e) 726 721 727 722 let test_invalid_digit () = 728 723 try ··· 731 726 with 732 727 | Punycode.Error (Punycode.Invalid_digit _) -> () 733 728 | Punycode.Error e -> 734 - fail (Format.asprintf "wrong error type: %a" Punycode.pp_error_reason e) 729 + fail (Fmt.str "wrong error type: %a" Punycode.pp_error_reason e) 735 730 736 731 let test_label_too_long () = 737 732 let long_label = String.make 100 'a' in ··· 741 736 with 742 737 | Punycode.Error (Punycode.Label_too_long _) -> () 743 738 | Punycode.Error e -> 744 - fail (Format.asprintf "wrong error type: %a" Punycode.pp_error_reason e) 739 + fail (Fmt.str "wrong error type: %a" Punycode.pp_error_reason e) 745 740 746 741 let test_empty_label () = 747 742 try ··· 750 745 with 751 746 | Punycode.Error Punycode.Empty_label -> () 752 747 | Punycode.Error e -> 753 - fail (Format.asprintf "wrong error type: %a" Punycode.pp_error_reason e) 748 + fail (Fmt.str "wrong error type: %a" Punycode.pp_error_reason e) 754 749 755 750 (* Validation tests *) 756 751 let test_is_basic () =
+9 -13
test/test_punycode_idna.ml
··· 14 14 let result = Punycode_idna.to_ascii input in 15 15 check string msg expected result 16 16 with Punycode_idna.Error e -> 17 - fail 18 - (Format.asprintf "%s: to_ascii failed: %a" msg 19 - Punycode_idna.pp_error_reason e) 17 + fail (Fmt.str "%s: to_ascii failed: %a" msg Punycode_idna.pp_error_reason e) 20 18 21 19 let check_to_unicode ~msg expected input = 22 20 try ··· 24 22 check string msg expected result 25 23 with Punycode_idna.Error e -> 26 24 fail 27 - (Format.asprintf "%s: to_unicode failed: %a" msg 28 - Punycode_idna.pp_error_reason e) 25 + (Fmt.str "%s: to_unicode failed: %a" msg Punycode_idna.pp_error_reason e) 29 26 30 27 let check_label_to_ascii ~msg expected input = 31 28 try ··· 33 30 check string msg expected result 34 31 with Punycode_idna.Error e -> 35 32 fail 36 - (Format.asprintf "%s: label_to_ascii failed: %a" msg 37 - Punycode_idna.pp_error_reason e) 33 + (Fmt.str "%s: label_to_ascii failed: %a" msg Punycode_idna.pp_error_reason 34 + e) 38 35 39 36 let check_label_to_unicode ~msg expected input = 40 37 try ··· 42 39 check string msg expected result 43 40 with Punycode_idna.Error e -> 44 41 fail 45 - (Format.asprintf "%s: label_to_unicode failed: %a" msg 42 + (Fmt.str "%s: label_to_unicode failed: %a" msg 46 43 Punycode_idna.pp_error_reason e) 47 44 48 45 let check_roundtrip ~msg input = ··· 52 49 check string msg input unicode 53 50 with Punycode_idna.Error e -> 54 51 fail 55 - (Format.asprintf "%s: roundtrip failed: %a" msg 56 - Punycode_idna.pp_error_reason e) 52 + (Fmt.str "%s: roundtrip failed: %a" msg Punycode_idna.pp_error_reason e) 57 53 58 54 let check_raises_error ~msg f = 59 55 try 60 56 ignore (f ()); 61 - fail (Printf.sprintf "%s: expected Error but succeeded" msg) 57 + fail (Fmt.str "%s: expected Error but succeeded" msg) 62 58 with Punycode_idna.Error _ -> () 63 59 64 60 (* {1 to_ascii Test Vectors} *) ··· 90 86 labels 91 87 with Punycode_idna.Error e -> 92 88 fail 93 - (Format.asprintf "multiple IDN labels: to_ascii failed: %a" 89 + (Fmt.str "multiple IDN labels: to_ascii failed: %a" 94 90 Punycode_idna.pp_error_reason e) 95 91 96 92 let test_to_ascii_chinese () = ··· 108 104 check string "TLD preserved" "ru" (List.nth labels 1) 109 105 with Punycode_idna.Error e -> 110 106 fail 111 - (Format.asprintf "Russian domain: to_ascii failed: %a" 107 + (Fmt.str "Russian domain: to_ascii failed: %a" 112 108 Punycode_idna.pp_error_reason e) 113 109 114 110 (* {1 to_unicode Test Vectors} *)