react-native-quick-crypto 1.0.19 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (561) hide show
  1. package/QuickCrypto.podspec +12 -38
  2. package/README.md +2 -0
  3. package/android/CMakeLists.txt +3 -0
  4. package/android/build.gradle +5 -1
  5. package/cpp/argon2/HybridArgon2.cpp +10 -3
  6. package/cpp/blake3/HybridBlake3.cpp +5 -3
  7. package/cpp/cipher/CCMCipher.cpp +29 -16
  8. package/cpp/cipher/CCMCipher.hpp +2 -4
  9. package/cpp/cipher/ChaCha20Cipher.cpp +14 -18
  10. package/cpp/cipher/ChaCha20Cipher.hpp +2 -4
  11. package/cpp/cipher/ChaCha20Poly1305Cipher.cpp +34 -23
  12. package/cpp/cipher/ChaCha20Poly1305Cipher.hpp +2 -4
  13. package/cpp/cipher/GCMCipher.cpp +14 -15
  14. package/cpp/cipher/HybridCipher.cpp +39 -36
  15. package/cpp/cipher/HybridCipher.hpp +17 -1
  16. package/cpp/cipher/HybridRsaCipher.cpp +74 -29
  17. package/cpp/cipher/OCBCipher.cpp +4 -3
  18. package/cpp/cipher/XChaCha20Poly1305Cipher.cpp +14 -13
  19. package/cpp/cipher/XSalsa20Cipher.cpp +72 -6
  20. package/cpp/cipher/XSalsa20Cipher.hpp +25 -3
  21. package/cpp/cipher/XSalsa20Poly1305Cipher.cpp +21 -25
  22. package/cpp/dh/HybridDiffieHellman.cpp +29 -0
  23. package/cpp/ec/HybridEcKeyPair.cpp +35 -33
  24. package/cpp/ec/HybridEcKeyPair.hpp +3 -7
  25. package/cpp/ecdh/HybridECDH.cpp +23 -0
  26. package/cpp/ed25519/HybridEdKeyPair.cpp +73 -117
  27. package/cpp/ed25519/HybridEdKeyPair.hpp +5 -9
  28. package/cpp/hash/HybridHash.cpp +5 -7
  29. package/cpp/hkdf/HybridHkdf.cpp +6 -4
  30. package/cpp/hmac/HybridHmac.cpp +4 -6
  31. package/cpp/kmac/HybridKmac.cpp +4 -4
  32. package/cpp/mldsa/HybridMlDsaKeyPair.cpp +37 -49
  33. package/cpp/mlkem/HybridMlKemKeyPair.cpp +39 -43
  34. package/cpp/pbkdf2/HybridPbkdf2.cpp +7 -8
  35. package/cpp/rsa/HybridRsaKeyPair.cpp +5 -8
  36. package/cpp/rsa/HybridRsaKeyPair.hpp +4 -7
  37. package/cpp/scrypt/HybridScrypt.cpp +6 -4
  38. package/cpp/sign/HybridSignHandle.cpp +25 -68
  39. package/cpp/sign/HybridVerifyHandle.cpp +23 -60
  40. package/cpp/utils/HybridUtils.cpp +213 -111
  41. package/cpp/utils/HybridUtils.hpp +9 -2
  42. package/cpp/utils/QuickCryptoUtils.hpp +72 -0
  43. package/deps/simdutf/LICENSE-APACHE +201 -0
  44. package/deps/simdutf/LICENSE-MIT +18 -0
  45. package/deps/simdutf/README.md +2782 -0
  46. package/deps/simdutf/include/simdutf/avx512.h +79 -0
  47. package/deps/simdutf/include/simdutf/base64_implementation.h +158 -0
  48. package/deps/simdutf/include/simdutf/base64_tables.h +887 -0
  49. package/deps/simdutf/include/simdutf/common_defs.h +186 -0
  50. package/deps/simdutf/include/simdutf/compiler_check.h +50 -0
  51. package/deps/simdutf/include/simdutf/constexpr_ptr.h +138 -0
  52. package/deps/simdutf/include/simdutf/encoding_types.h +189 -0
  53. package/deps/simdutf/include/simdutf/error.h +126 -0
  54. package/deps/simdutf/include/simdutf/implementation.h +7081 -0
  55. package/deps/simdutf/include/simdutf/internal/isadetection.h +325 -0
  56. package/deps/simdutf/include/simdutf/portability.h +285 -0
  57. package/deps/simdutf/include/simdutf/scalar/ascii.h +86 -0
  58. package/deps/simdutf/include/simdutf/scalar/atomic_util.h +105 -0
  59. package/deps/simdutf/include/simdutf/scalar/base64.h +911 -0
  60. package/deps/simdutf/include/simdutf/scalar/latin1.h +26 -0
  61. package/deps/simdutf/include/simdutf/scalar/latin1_to_utf16/latin1_to_utf16.h +52 -0
  62. package/deps/simdutf/include/simdutf/scalar/latin1_to_utf32/latin1_to_utf32.h +27 -0
  63. package/deps/simdutf/include/simdutf/scalar/latin1_to_utf8/latin1_to_utf8.h +191 -0
  64. package/deps/simdutf/include/simdutf/scalar/swap_bytes.h +35 -0
  65. package/deps/simdutf/include/simdutf/scalar/utf16.h +226 -0
  66. package/deps/simdutf/include/simdutf/scalar/utf16_to_latin1/utf16_to_latin1.h +108 -0
  67. package/deps/simdutf/include/simdutf/scalar/utf16_to_latin1/valid_utf16_to_latin1.h +40 -0
  68. package/deps/simdutf/include/simdutf/scalar/utf16_to_utf32/utf16_to_utf32.h +86 -0
  69. package/deps/simdutf/include/simdutf/scalar/utf16_to_utf32/valid_utf16_to_utf32.h +44 -0
  70. package/deps/simdutf/include/simdutf/scalar/utf16_to_utf8/utf16_to_utf8.h +295 -0
  71. package/deps/simdutf/include/simdutf/scalar/utf16_to_utf8/valid_utf16_to_utf8.h +91 -0
  72. package/deps/simdutf/include/simdutf/scalar/utf32.h +82 -0
  73. package/deps/simdutf/include/simdutf/scalar/utf32_to_latin1/utf32_to_latin1.h +68 -0
  74. package/deps/simdutf/include/simdutf/scalar/utf32_to_latin1/valid_utf32_to_latin1.h +67 -0
  75. package/deps/simdutf/include/simdutf/scalar/utf32_to_utf16/utf32_to_utf16.h +84 -0
  76. package/deps/simdutf/include/simdutf/scalar/utf32_to_utf16/valid_utf32_to_utf16.h +44 -0
  77. package/deps/simdutf/include/simdutf/scalar/utf32_to_utf8/utf32_to_utf8.h +142 -0
  78. package/deps/simdutf/include/simdutf/scalar/utf32_to_utf8/valid_utf32_to_utf8.h +72 -0
  79. package/deps/simdutf/include/simdutf/scalar/utf8.h +326 -0
  80. package/deps/simdutf/include/simdutf/scalar/utf8_to_latin1/utf8_to_latin1.h +225 -0
  81. package/deps/simdutf/include/simdutf/scalar/utf8_to_latin1/valid_utf8_to_latin1.h +87 -0
  82. package/deps/simdutf/include/simdutf/scalar/utf8_to_utf16/utf8_to_utf16.h +342 -0
  83. package/deps/simdutf/include/simdutf/scalar/utf8_to_utf16/valid_utf8_to_utf16.h +106 -0
  84. package/deps/simdutf/include/simdutf/scalar/utf8_to_utf32/utf8_to_utf32.h +299 -0
  85. package/deps/simdutf/include/simdutf/scalar/utf8_to_utf32/valid_utf8_to_utf32.h +83 -0
  86. package/deps/simdutf/include/simdutf/simdutf_version.h +26 -0
  87. package/deps/simdutf/include/simdutf.h +26 -0
  88. package/deps/simdutf/include/simdutf_c.h +342 -0
  89. package/deps/simdutf/src/arm64/arm_base64.cpp +791 -0
  90. package/deps/simdutf/src/arm64/arm_convert_latin1_to_utf16.cpp +24 -0
  91. package/deps/simdutf/src/arm64/arm_convert_latin1_to_utf32.cpp +24 -0
  92. package/deps/simdutf/src/arm64/arm_convert_latin1_to_utf8.cpp +70 -0
  93. package/deps/simdutf/src/arm64/arm_convert_utf16_to_latin1.cpp +61 -0
  94. package/deps/simdutf/src/arm64/arm_convert_utf16_to_utf32.cpp +185 -0
  95. package/deps/simdutf/src/arm64/arm_convert_utf16_to_utf8.cpp +780 -0
  96. package/deps/simdutf/src/arm64/arm_convert_utf32_to_latin1.cpp +60 -0
  97. package/deps/simdutf/src/arm64/arm_convert_utf32_to_utf16.cpp +208 -0
  98. package/deps/simdutf/src/arm64/arm_convert_utf32_to_utf8.cpp +505 -0
  99. package/deps/simdutf/src/arm64/arm_convert_utf8_to_latin1.cpp +69 -0
  100. package/deps/simdutf/src/arm64/arm_convert_utf8_to_utf16.cpp +313 -0
  101. package/deps/simdutf/src/arm64/arm_convert_utf8_to_utf32.cpp +179 -0
  102. package/deps/simdutf/src/arm64/arm_find.cpp +199 -0
  103. package/deps/simdutf/src/arm64/arm_utf16fix.cpp +185 -0
  104. package/deps/simdutf/src/arm64/arm_validate_utf16.cpp +165 -0
  105. package/deps/simdutf/src/arm64/arm_validate_utf32le.cpp +65 -0
  106. package/deps/simdutf/src/arm64/implementation.cpp +1442 -0
  107. package/deps/simdutf/src/encoding_types.cpp +67 -0
  108. package/deps/simdutf/src/error.cpp +3 -0
  109. package/deps/simdutf/src/fallback/implementation.cpp +589 -0
  110. package/deps/simdutf/src/generic/ascii_validation.h +50 -0
  111. package/deps/simdutf/src/generic/base64.h +233 -0
  112. package/deps/simdutf/src/generic/base64lengths.h +63 -0
  113. package/deps/simdutf/src/generic/buf_block_reader.h +109 -0
  114. package/deps/simdutf/src/generic/find.h +75 -0
  115. package/deps/simdutf/src/generic/utf16/change_endianness.h +24 -0
  116. package/deps/simdutf/src/generic/utf16/count_code_points_bytemask.h +58 -0
  117. package/deps/simdutf/src/generic/utf16/to_well_formed.h +93 -0
  118. package/deps/simdutf/src/generic/utf16/utf32_length_from_utf16.h +15 -0
  119. package/deps/simdutf/src/generic/utf16/utf8_length_from_utf16.h +35 -0
  120. package/deps/simdutf/src/generic/utf16/utf8_length_from_utf16_bytemask.h +199 -0
  121. package/deps/simdutf/src/generic/utf16.h +73 -0
  122. package/deps/simdutf/src/generic/utf32.h +136 -0
  123. package/deps/simdutf/src/generic/utf8/utf16_length_from_utf8_bytemask.h +53 -0
  124. package/deps/simdutf/src/generic/utf8.h +92 -0
  125. package/deps/simdutf/src/generic/utf8_to_latin1/utf8_to_latin1.h +316 -0
  126. package/deps/simdutf/src/generic/utf8_to_latin1/valid_utf8_to_latin1.h +78 -0
  127. package/deps/simdutf/src/generic/utf8_to_utf16/utf8_to_utf16.h +332 -0
  128. package/deps/simdutf/src/generic/utf8_to_utf16/valid_utf8_to_utf16.h +74 -0
  129. package/deps/simdutf/src/generic/utf8_to_utf32/utf8_to_utf32.h +318 -0
  130. package/deps/simdutf/src/generic/utf8_to_utf32/valid_utf8_to_utf32.h +42 -0
  131. package/deps/simdutf/src/generic/utf8_validation/utf8_lookup4_algorithm.h +223 -0
  132. package/deps/simdutf/src/generic/utf8_validation/utf8_validator.h +84 -0
  133. package/deps/simdutf/src/generic/validate_utf16.h +164 -0
  134. package/deps/simdutf/src/generic/validate_utf32.h +99 -0
  135. package/deps/simdutf/src/haswell/avx2_base64.cpp +837 -0
  136. package/deps/simdutf/src/haswell/avx2_convert_latin1_to_utf16.cpp +28 -0
  137. package/deps/simdutf/src/haswell/avx2_convert_latin1_to_utf32.cpp +20 -0
  138. package/deps/simdutf/src/haswell/avx2_convert_latin1_to_utf8.cpp +83 -0
  139. package/deps/simdutf/src/haswell/avx2_convert_utf16_to_latin1.cpp +83 -0
  140. package/deps/simdutf/src/haswell/avx2_convert_utf16_to_utf32.cpp +210 -0
  141. package/deps/simdutf/src/haswell/avx2_convert_utf16_to_utf8.cpp +602 -0
  142. package/deps/simdutf/src/haswell/avx2_convert_utf32_to_latin1.cpp +116 -0
  143. package/deps/simdutf/src/haswell/avx2_convert_utf32_to_utf16.cpp +164 -0
  144. package/deps/simdutf/src/haswell/avx2_convert_utf32_to_utf8.cpp +569 -0
  145. package/deps/simdutf/src/haswell/avx2_convert_utf8_to_latin1.cpp +60 -0
  146. package/deps/simdutf/src/haswell/avx2_convert_utf8_to_utf16.cpp +195 -0
  147. package/deps/simdutf/src/haswell/avx2_convert_utf8_to_utf32.cpp +135 -0
  148. package/deps/simdutf/src/haswell/avx2_utf16fix.cpp +173 -0
  149. package/deps/simdutf/src/haswell/avx2_validate_utf16.cpp +17 -0
  150. package/deps/simdutf/src/haswell/implementation.cpp +1447 -0
  151. package/deps/simdutf/src/icelake/icelake_ascii_validation.inl.cpp +19 -0
  152. package/deps/simdutf/src/icelake/icelake_base64.inl.cpp +630 -0
  153. package/deps/simdutf/src/icelake/icelake_common.inl.cpp +37 -0
  154. package/deps/simdutf/src/icelake/icelake_convert_latin1_to_utf16.inl.cpp +36 -0
  155. package/deps/simdutf/src/icelake/icelake_convert_latin1_to_utf32.inl.cpp +23 -0
  156. package/deps/simdutf/src/icelake/icelake_convert_latin1_to_utf8.inl.cpp +107 -0
  157. package/deps/simdutf/src/icelake/icelake_convert_utf16_to_latin1.inl.cpp +103 -0
  158. package/deps/simdutf/src/icelake/icelake_convert_utf16_to_utf32.inl.cpp +136 -0
  159. package/deps/simdutf/src/icelake/icelake_convert_utf16_to_utf8.inl.cpp +206 -0
  160. package/deps/simdutf/src/icelake/icelake_convert_utf32_to_latin1.inl.cpp +74 -0
  161. package/deps/simdutf/src/icelake/icelake_convert_utf32_to_utf16.inl.cpp +338 -0
  162. package/deps/simdutf/src/icelake/icelake_convert_utf32_to_utf8.inl.cpp +574 -0
  163. package/deps/simdutf/src/icelake/icelake_convert_utf8_to_latin1.inl.cpp +104 -0
  164. package/deps/simdutf/src/icelake/icelake_convert_utf8_to_utf16.inl.cpp +75 -0
  165. package/deps/simdutf/src/icelake/icelake_convert_valid_utf8_to_latin1.inl.cpp +69 -0
  166. package/deps/simdutf/src/icelake/icelake_find.inl.cpp +146 -0
  167. package/deps/simdutf/src/icelake/icelake_from_utf8.inl.cpp +266 -0
  168. package/deps/simdutf/src/icelake/icelake_from_valid_utf8.inl.cpp +136 -0
  169. package/deps/simdutf/src/icelake/icelake_macros.inl.cpp +143 -0
  170. package/deps/simdutf/src/icelake/icelake_utf16fix.cpp +138 -0
  171. package/deps/simdutf/src/icelake/icelake_utf32_validation.inl.cpp +63 -0
  172. package/deps/simdutf/src/icelake/icelake_utf8_common.inl.cpp +753 -0
  173. package/deps/simdutf/src/icelake/icelake_utf8_length_from_utf16.inl.cpp +269 -0
  174. package/deps/simdutf/src/icelake/icelake_utf8_validation.inl.cpp +116 -0
  175. package/deps/simdutf/src/icelake/implementation.cpp +1903 -0
  176. package/deps/simdutf/src/implementation.cpp +2526 -0
  177. package/deps/simdutf/src/lasx/implementation.cpp +1531 -0
  178. package/deps/simdutf/src/lasx/lasx_base64.cpp +695 -0
  179. package/deps/simdutf/src/lasx/lasx_convert_latin1_to_utf16.cpp +76 -0
  180. package/deps/simdutf/src/lasx/lasx_convert_latin1_to_utf32.cpp +55 -0
  181. package/deps/simdutf/src/lasx/lasx_convert_latin1_to_utf8.cpp +65 -0
  182. package/deps/simdutf/src/lasx/lasx_convert_utf16_to_latin1.cpp +64 -0
  183. package/deps/simdutf/src/lasx/lasx_convert_utf16_to_utf32.cpp +183 -0
  184. package/deps/simdutf/src/lasx/lasx_convert_utf16_to_utf8.cpp +550 -0
  185. package/deps/simdutf/src/lasx/lasx_convert_utf32_to_latin1.cpp +73 -0
  186. package/deps/simdutf/src/lasx/lasx_convert_utf32_to_utf16.cpp +218 -0
  187. package/deps/simdutf/src/lasx/lasx_convert_utf32_to_utf8.cpp +589 -0
  188. package/deps/simdutf/src/lasx/lasx_convert_utf8_to_latin1.cpp +72 -0
  189. package/deps/simdutf/src/lasx/lasx_convert_utf8_to_utf16.cpp +296 -0
  190. package/deps/simdutf/src/lasx/lasx_convert_utf8_to_utf32.cpp +190 -0
  191. package/deps/simdutf/src/lasx/lasx_find.cpp +64 -0
  192. package/deps/simdutf/src/lasx/lasx_validate_utf16.cpp +13 -0
  193. package/deps/simdutf/src/lasx/lasx_validate_utf32le.cpp +84 -0
  194. package/deps/simdutf/src/lsx/implementation.cpp +1417 -0
  195. package/deps/simdutf/src/lsx/lsx_base64.cpp +675 -0
  196. package/deps/simdutf/src/lsx/lsx_convert_latin1_to_utf16.cpp +39 -0
  197. package/deps/simdutf/src/lsx/lsx_convert_latin1_to_utf32.cpp +27 -0
  198. package/deps/simdutf/src/lsx/lsx_convert_latin1_to_utf8.cpp +56 -0
  199. package/deps/simdutf/src/lsx/lsx_convert_utf16_to_latin1.cpp +64 -0
  200. package/deps/simdutf/src/lsx/lsx_convert_utf16_to_utf32.cpp +133 -0
  201. package/deps/simdutf/src/lsx/lsx_convert_utf16_to_utf8.cpp +518 -0
  202. package/deps/simdutf/src/lsx/lsx_convert_utf32_to_latin1.cpp +66 -0
  203. package/deps/simdutf/src/lsx/lsx_convert_utf32_to_utf16.cpp +155 -0
  204. package/deps/simdutf/src/lsx/lsx_convert_utf32_to_utf8.cpp +459 -0
  205. package/deps/simdutf/src/lsx/lsx_convert_utf8_to_latin1.cpp +75 -0
  206. package/deps/simdutf/src/lsx/lsx_convert_utf8_to_utf16.cpp +291 -0
  207. package/deps/simdutf/src/lsx/lsx_convert_utf8_to_utf32.cpp +179 -0
  208. package/deps/simdutf/src/lsx/lsx_find.cpp +60 -0
  209. package/deps/simdutf/src/lsx/lsx_validate_utf16.cpp +13 -0
  210. package/deps/simdutf/src/lsx/lsx_validate_utf32le.cpp +68 -0
  211. package/deps/simdutf/src/ppc64/implementation.cpp +992 -0
  212. package/deps/simdutf/src/ppc64/ppc64_base64.cpp +480 -0
  213. package/deps/simdutf/src/ppc64/ppc64_base64_internal_tests.cpp +401 -0
  214. package/deps/simdutf/src/ppc64/ppc64_convert_latin1_to_utf16.cpp +12 -0
  215. package/deps/simdutf/src/ppc64/ppc64_convert_latin1_to_utf32.cpp +12 -0
  216. package/deps/simdutf/src/ppc64/ppc64_convert_latin1_to_utf8.cpp +149 -0
  217. package/deps/simdutf/src/ppc64/ppc64_convert_utf16_to_latin1.cpp +67 -0
  218. package/deps/simdutf/src/ppc64/ppc64_convert_utf16_to_utf32.cpp +87 -0
  219. package/deps/simdutf/src/ppc64/ppc64_convert_utf16_to_utf8.cpp +296 -0
  220. package/deps/simdutf/src/ppc64/ppc64_convert_utf32_to_latin1.cpp +57 -0
  221. package/deps/simdutf/src/ppc64/ppc64_convert_utf32_to_utf16.cpp +117 -0
  222. package/deps/simdutf/src/ppc64/ppc64_convert_utf32_to_utf8.cpp +166 -0
  223. package/deps/simdutf/src/ppc64/ppc64_convert_utf8_to_latin1.cpp +69 -0
  224. package/deps/simdutf/src/ppc64/ppc64_convert_utf8_to_utf16.cpp +211 -0
  225. package/deps/simdutf/src/ppc64/ppc64_convert_utf8_to_utf32.cpp +153 -0
  226. package/deps/simdutf/src/ppc64/ppc64_utf16_to_utf8_tables.h +1011 -0
  227. package/deps/simdutf/src/ppc64/ppc64_utf8_length_from_latin1.cpp +37 -0
  228. package/deps/simdutf/src/ppc64/ppc64_validate_utf16.cpp +19 -0
  229. package/deps/simdutf/src/ppc64/templates.cpp +91 -0
  230. package/deps/simdutf/src/rvv/implementation.cpp +138 -0
  231. package/deps/simdutf/src/rvv/rvv_find.cpp +27 -0
  232. package/deps/simdutf/src/rvv/rvv_helpers.inl.cpp +23 -0
  233. package/deps/simdutf/src/rvv/rvv_latin1_to.inl.cpp +71 -0
  234. package/deps/simdutf/src/rvv/rvv_length_from.inl.cpp +164 -0
  235. package/deps/simdutf/src/rvv/rvv_utf16_to.inl.cpp +399 -0
  236. package/deps/simdutf/src/rvv/rvv_utf16fix.cpp +110 -0
  237. package/deps/simdutf/src/rvv/rvv_utf32_to.inl.cpp +307 -0
  238. package/deps/simdutf/src/rvv/rvv_utf8_to.inl.cpp +435 -0
  239. package/deps/simdutf/src/rvv/rvv_validate.inl.cpp +275 -0
  240. package/deps/simdutf/src/simdutf/arm64/begin.h +2 -0
  241. package/deps/simdutf/src/simdutf/arm64/bitmanipulation.h +34 -0
  242. package/deps/simdutf/src/simdutf/arm64/end.h +2 -0
  243. package/deps/simdutf/src/simdutf/arm64/implementation.h +307 -0
  244. package/deps/simdutf/src/simdutf/arm64/intrinsics.h +10 -0
  245. package/deps/simdutf/src/simdutf/arm64/simd.h +547 -0
  246. package/deps/simdutf/src/simdutf/arm64/simd16-inl.h +403 -0
  247. package/deps/simdutf/src/simdutf/arm64/simd32-inl.h +129 -0
  248. package/deps/simdutf/src/simdutf/arm64/simd64-inl.h +28 -0
  249. package/deps/simdutf/src/simdutf/arm64.h +43 -0
  250. package/deps/simdutf/src/simdutf/fallback/begin.h +1 -0
  251. package/deps/simdutf/src/simdutf/fallback/bitmanipulation.h +13 -0
  252. package/deps/simdutf/src/simdutf/fallback/end.h +1 -0
  253. package/deps/simdutf/src/simdutf/fallback/implementation.h +331 -0
  254. package/deps/simdutf/src/simdutf/fallback.h +42 -0
  255. package/deps/simdutf/src/simdutf/haswell/begin.h +15 -0
  256. package/deps/simdutf/src/simdutf/haswell/bitmanipulation.h +35 -0
  257. package/deps/simdutf/src/simdutf/haswell/end.h +13 -0
  258. package/deps/simdutf/src/simdutf/haswell/implementation.h +338 -0
  259. package/deps/simdutf/src/simdutf/haswell/intrinsics.h +67 -0
  260. package/deps/simdutf/src/simdutf/haswell/simd.h +363 -0
  261. package/deps/simdutf/src/simdutf/haswell/simd16-inl.h +261 -0
  262. package/deps/simdutf/src/simdutf/haswell/simd32-inl.h +111 -0
  263. package/deps/simdutf/src/simdutf/haswell/simd64-inl.h +34 -0
  264. package/deps/simdutf/src/simdutf/haswell.h +63 -0
  265. package/deps/simdutf/src/simdutf/icelake/begin.h +14 -0
  266. package/deps/simdutf/src/simdutf/icelake/bitmanipulation.h +44 -0
  267. package/deps/simdutf/src/simdutf/icelake/end.h +12 -0
  268. package/deps/simdutf/src/simdutf/icelake/implementation.h +346 -0
  269. package/deps/simdutf/src/simdutf/icelake/intrinsics.h +138 -0
  270. package/deps/simdutf/src/simdutf/icelake/simd.h +17 -0
  271. package/deps/simdutf/src/simdutf/icelake/simd16-inl.h +90 -0
  272. package/deps/simdutf/src/simdutf/icelake/simd32-inl.h +47 -0
  273. package/deps/simdutf/src/simdutf/icelake.h +81 -0
  274. package/deps/simdutf/src/simdutf/lasx/begin.h +8 -0
  275. package/deps/simdutf/src/simdutf/lasx/bitmanipulation.h +25 -0
  276. package/deps/simdutf/src/simdutf/lasx/end.h +8 -0
  277. package/deps/simdutf/src/simdutf/lasx/implementation.h +310 -0
  278. package/deps/simdutf/src/simdutf/lasx/intrinsics.h +319 -0
  279. package/deps/simdutf/src/simdutf/lasx/simd.h +551 -0
  280. package/deps/simdutf/src/simdutf/lasx/simd16-inl.h +234 -0
  281. package/deps/simdutf/src/simdutf/lasx/simd32-inl.h +74 -0
  282. package/deps/simdutf/src/simdutf/lasx/simd64-inl.h +52 -0
  283. package/deps/simdutf/src/simdutf/lasx.h +49 -0
  284. package/deps/simdutf/src/simdutf/lsx/begin.h +2 -0
  285. package/deps/simdutf/src/simdutf/lsx/bitmanipulation.h +25 -0
  286. package/deps/simdutf/src/simdutf/lsx/end.h +2 -0
  287. package/deps/simdutf/src/simdutf/lsx/implementation.h +309 -0
  288. package/deps/simdutf/src/simdutf/lsx/intrinsics.h +196 -0
  289. package/deps/simdutf/src/simdutf/lsx/simd.h +421 -0
  290. package/deps/simdutf/src/simdutf/lsx/simd16-inl.h +242 -0
  291. package/deps/simdutf/src/simdutf/lsx/simd32-inl.h +69 -0
  292. package/deps/simdutf/src/simdutf/lsx/simd64-inl.h +50 -0
  293. package/deps/simdutf/src/simdutf/lsx.h +52 -0
  294. package/deps/simdutf/src/simdutf/ppc64/begin.h +1 -0
  295. package/deps/simdutf/src/simdutf/ppc64/bitmanipulation.h +29 -0
  296. package/deps/simdutf/src/simdutf/ppc64/end.h +1 -0
  297. package/deps/simdutf/src/simdutf/ppc64/implementation.h +348 -0
  298. package/deps/simdutf/src/simdutf/ppc64/intrinsics.h +19 -0
  299. package/deps/simdutf/src/simdutf/ppc64/simd.h +177 -0
  300. package/deps/simdutf/src/simdutf/ppc64/simd16-inl.h +327 -0
  301. package/deps/simdutf/src/simdutf/ppc64/simd32-inl.h +247 -0
  302. package/deps/simdutf/src/simdutf/ppc64/simd8-inl.h +618 -0
  303. package/deps/simdutf/src/simdutf/ppc64.h +40 -0
  304. package/deps/simdutf/src/simdutf/rvv/begin.h +7 -0
  305. package/deps/simdutf/src/simdutf/rvv/end.h +7 -0
  306. package/deps/simdutf/src/simdutf/rvv/implementation.h +321 -0
  307. package/deps/simdutf/src/simdutf/rvv/intrinsics.h +131 -0
  308. package/deps/simdutf/src/simdutf/rvv.h +41 -0
  309. package/deps/simdutf/src/simdutf/westmere/begin.h +8 -0
  310. package/deps/simdutf/src/simdutf/westmere/bitmanipulation.h +37 -0
  311. package/deps/simdutf/src/simdutf/westmere/end.h +8 -0
  312. package/deps/simdutf/src/simdutf/westmere/implementation.h +338 -0
  313. package/deps/simdutf/src/simdutf/westmere/intrinsics.h +38 -0
  314. package/deps/simdutf/src/simdutf/westmere/simd.h +379 -0
  315. package/deps/simdutf/src/simdutf/westmere/simd16-inl.h +242 -0
  316. package/deps/simdutf/src/simdutf/westmere/simd32-inl.h +151 -0
  317. package/deps/simdutf/src/simdutf/westmere/simd64-inl.h +33 -0
  318. package/deps/simdutf/src/simdutf/westmere.h +59 -0
  319. package/deps/simdutf/src/simdutf.cpp +152 -0
  320. package/deps/simdutf/src/simdutf_c.cpp +525 -0
  321. package/deps/simdutf/src/tables/utf16_to_utf8_tables.h +768 -0
  322. package/deps/simdutf/src/tables/utf32_to_utf16_tables.h +53 -0
  323. package/deps/simdutf/src/tables/utf8_to_utf16_tables.h +826 -0
  324. package/deps/simdutf/src/westmere/implementation.cpp +1479 -0
  325. package/deps/simdutf/src/westmere/internal/loader.cpp +7 -0
  326. package/deps/simdutf/src/westmere/internal/write_v_u16_11bits_to_utf8.cpp +66 -0
  327. package/deps/simdutf/src/westmere/sse_base64.cpp +672 -0
  328. package/deps/simdutf/src/westmere/sse_convert_latin1_to_utf16.cpp +21 -0
  329. package/deps/simdutf/src/westmere/sse_convert_latin1_to_utf32.cpp +31 -0
  330. package/deps/simdutf/src/westmere/sse_convert_latin1_to_utf8.cpp +71 -0
  331. package/deps/simdutf/src/westmere/sse_convert_utf16_to_latin1.cpp +70 -0
  332. package/deps/simdutf/src/westmere/sse_convert_utf16_to_utf32.cpp +206 -0
  333. package/deps/simdutf/src/westmere/sse_convert_utf16_to_utf8.cpp +504 -0
  334. package/deps/simdutf/src/westmere/sse_convert_utf32_to_latin1.cpp +82 -0
  335. package/deps/simdutf/src/westmere/sse_convert_utf32_to_utf16.cpp +209 -0
  336. package/deps/simdutf/src/westmere/sse_convert_utf32_to_utf8.cpp +589 -0
  337. package/deps/simdutf/src/westmere/sse_convert_utf8_to_latin1.cpp +58 -0
  338. package/deps/simdutf/src/westmere/sse_convert_utf8_to_utf16.cpp +197 -0
  339. package/deps/simdutf/src/westmere/sse_convert_utf8_to_utf32.cpp +141 -0
  340. package/deps/simdutf/src/westmere/sse_utf16fix.cpp +82 -0
  341. package/deps/simdutf/src/westmere/sse_validate_utf16.cpp +17 -0
  342. package/lib/commonjs/argon2.js +51 -2
  343. package/lib/commonjs/argon2.js.map +1 -1
  344. package/lib/commonjs/cipher.js +109 -11
  345. package/lib/commonjs/cipher.js.map +1 -1
  346. package/lib/commonjs/dsa.js +8 -2
  347. package/lib/commonjs/dsa.js.map +1 -1
  348. package/lib/commonjs/hash.js +15 -5
  349. package/lib/commonjs/hash.js.map +1 -1
  350. package/lib/commonjs/hkdf.js +33 -6
  351. package/lib/commonjs/hkdf.js.map +1 -1
  352. package/lib/commonjs/hmac.js +15 -5
  353. package/lib/commonjs/hmac.js.map +1 -1
  354. package/lib/commonjs/keys/publicCipher.js +10 -4
  355. package/lib/commonjs/keys/publicCipher.js.map +1 -1
  356. package/lib/commonjs/random.js +11 -2
  357. package/lib/commonjs/random.js.map +1 -1
  358. package/lib/commonjs/rsa.js +12 -5
  359. package/lib/commonjs/rsa.js.map +1 -1
  360. package/lib/commonjs/scrypt.js +47 -6
  361. package/lib/commonjs/scrypt.js.map +1 -1
  362. package/lib/commonjs/subtle.js +76 -5
  363. package/lib/commonjs/subtle.js.map +1 -1
  364. package/lib/commonjs/utils/cipher.js +18 -7
  365. package/lib/commonjs/utils/cipher.js.map +1 -1
  366. package/lib/commonjs/utils/conversion.js +33 -9
  367. package/lib/commonjs/utils/conversion.js.map +1 -1
  368. package/lib/commonjs/utils/timingSafeEqual.js +7 -2
  369. package/lib/commonjs/utils/timingSafeEqual.js.map +1 -1
  370. package/lib/commonjs/x509certificate.js +6 -6
  371. package/lib/commonjs/x509certificate.js.map +1 -1
  372. package/lib/module/argon2.js +51 -2
  373. package/lib/module/argon2.js.map +1 -1
  374. package/lib/module/cipher.js +109 -11
  375. package/lib/module/cipher.js.map +1 -1
  376. package/lib/module/dsa.js +8 -2
  377. package/lib/module/dsa.js.map +1 -1
  378. package/lib/module/hash.js +15 -5
  379. package/lib/module/hash.js.map +1 -1
  380. package/lib/module/hkdf.js +33 -6
  381. package/lib/module/hkdf.js.map +1 -1
  382. package/lib/module/hmac.js +15 -5
  383. package/lib/module/hmac.js.map +1 -1
  384. package/lib/module/keys/publicCipher.js +10 -4
  385. package/lib/module/keys/publicCipher.js.map +1 -1
  386. package/lib/module/random.js +11 -2
  387. package/lib/module/random.js.map +1 -1
  388. package/lib/module/rsa.js +11 -4
  389. package/lib/module/rsa.js.map +1 -1
  390. package/lib/module/scrypt.js +47 -6
  391. package/lib/module/scrypt.js.map +1 -1
  392. package/lib/module/subtle.js +76 -5
  393. package/lib/module/subtle.js.map +1 -1
  394. package/lib/module/utils/cipher.js +18 -7
  395. package/lib/module/utils/cipher.js.map +1 -1
  396. package/lib/module/utils/conversion.js +33 -9
  397. package/lib/module/utils/conversion.js.map +1 -1
  398. package/lib/module/utils/timingSafeEqual.js +8 -3
  399. package/lib/module/utils/timingSafeEqual.js.map +1 -1
  400. package/lib/module/x509certificate.js +6 -6
  401. package/lib/module/x509certificate.js.map +1 -1
  402. package/lib/typescript/argon2.d.ts.map +1 -1
  403. package/lib/typescript/cipher.d.ts +2 -2
  404. package/lib/typescript/cipher.d.ts.map +1 -1
  405. package/lib/typescript/dsa.d.ts.map +1 -1
  406. package/lib/typescript/hash.d.ts +2 -2
  407. package/lib/typescript/hash.d.ts.map +1 -1
  408. package/lib/typescript/hkdf.d.ts.map +1 -1
  409. package/lib/typescript/hmac.d.ts +2 -2
  410. package/lib/typescript/hmac.d.ts.map +1 -1
  411. package/lib/typescript/index.d.ts +1 -1
  412. package/lib/typescript/index.d.ts.map +1 -1
  413. package/lib/typescript/keys/publicCipher.d.ts.map +1 -1
  414. package/lib/typescript/random.d.ts.map +1 -1
  415. package/lib/typescript/rsa.d.ts.map +1 -1
  416. package/lib/typescript/scrypt.d.ts.map +1 -1
  417. package/lib/typescript/specs/utils.nitro.d.ts +0 -2
  418. package/lib/typescript/specs/utils.nitro.d.ts.map +1 -1
  419. package/lib/typescript/subtle.d.ts.map +1 -1
  420. package/lib/typescript/utils/cipher.d.ts +13 -1
  421. package/lib/typescript/utils/cipher.d.ts.map +1 -1
  422. package/lib/typescript/utils/conversion.d.ts +9 -6
  423. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  424. package/lib/typescript/utils/timingSafeEqual.d.ts.map +1 -1
  425. package/lib/typescript/x509certificate.d.ts.map +1 -1
  426. package/nitrogen/generated/shared/c++/HybridUtilsSpec.cpp +0 -2
  427. package/nitrogen/generated/shared/c++/HybridUtilsSpec.hpp +0 -3
  428. package/package.json +38 -6
  429. package/src/argon2.ts +80 -2
  430. package/src/cipher.ts +139 -15
  431. package/src/dsa.ts +11 -2
  432. package/src/hash.ts +17 -7
  433. package/src/hkdf.ts +44 -6
  434. package/src/hmac.ts +17 -7
  435. package/src/keys/publicCipher.ts +10 -4
  436. package/src/random.ts +11 -2
  437. package/src/rsa.ts +18 -4
  438. package/src/scrypt.ts +73 -6
  439. package/src/specs/utils.nitro.ts +0 -2
  440. package/src/subtle.ts +90 -8
  441. package/src/utils/cipher.ts +30 -8
  442. package/src/utils/conversion.ts +58 -20
  443. package/src/utils/timingSafeEqual.ts +8 -3
  444. package/src/x509certificate.ts +5 -6
  445. package/deps/blake3/.cargo/config.toml +0 -2
  446. package/deps/blake3/.git-blame-ignore-revs +0 -2
  447. package/deps/blake3/.github/workflows/build_b3sum.py +0 -38
  448. package/deps/blake3/.github/workflows/ci.yml +0 -491
  449. package/deps/blake3/.github/workflows/tag.yml +0 -43
  450. package/deps/blake3/.github/workflows/upload_github_release_asset.py +0 -73
  451. package/deps/blake3/CONTRIBUTING.md +0 -31
  452. package/deps/blake3/Cargo.toml +0 -135
  453. package/deps/blake3/b3sum/Cargo.lock +0 -513
  454. package/deps/blake3/b3sum/Cargo.toml +0 -26
  455. package/deps/blake3/b3sum/README.md +0 -72
  456. package/deps/blake3/b3sum/src/main.rs +0 -564
  457. package/deps/blake3/b3sum/src/unit_tests.rs +0 -235
  458. package/deps/blake3/b3sum/tests/cli_tests.rs +0 -680
  459. package/deps/blake3/b3sum/what_does_check_do.md +0 -176
  460. package/deps/blake3/benches/bench.rs +0 -623
  461. package/deps/blake3/build.rs +0 -389
  462. package/deps/blake3/c/CMakeLists.txt +0 -383
  463. package/deps/blake3/c/CMakePresets.json +0 -73
  464. package/deps/blake3/c/Makefile.testing +0 -82
  465. package/deps/blake3/c/blake3-config.cmake.in +0 -14
  466. package/deps/blake3/c/blake3_avx2.c +0 -326
  467. package/deps/blake3/c/blake3_avx2_x86-64_unix.S +0 -1815
  468. package/deps/blake3/c/blake3_avx2_x86-64_windows_gnu.S +0 -1817
  469. package/deps/blake3/c/blake3_avx2_x86-64_windows_msvc.asm +0 -1828
  470. package/deps/blake3/c/blake3_avx512.c +0 -1388
  471. package/deps/blake3/c/blake3_avx512_x86-64_unix.S +0 -4824
  472. package/deps/blake3/c/blake3_avx512_x86-64_windows_gnu.S +0 -2615
  473. package/deps/blake3/c/blake3_avx512_x86-64_windows_msvc.asm +0 -2634
  474. package/deps/blake3/c/blake3_c_rust_bindings/Cargo.toml +0 -32
  475. package/deps/blake3/c/blake3_c_rust_bindings/README.md +0 -4
  476. package/deps/blake3/c/blake3_c_rust_bindings/benches/bench.rs +0 -477
  477. package/deps/blake3/c/blake3_c_rust_bindings/build.rs +0 -253
  478. package/deps/blake3/c/blake3_c_rust_bindings/cross_test.sh +0 -31
  479. package/deps/blake3/c/blake3_c_rust_bindings/src/lib.rs +0 -333
  480. package/deps/blake3/c/blake3_c_rust_bindings/src/test.rs +0 -696
  481. package/deps/blake3/c/blake3_sse2.c +0 -566
  482. package/deps/blake3/c/blake3_sse2_x86-64_unix.S +0 -2291
  483. package/deps/blake3/c/blake3_sse2_x86-64_windows_gnu.S +0 -2332
  484. package/deps/blake3/c/blake3_sse2_x86-64_windows_msvc.asm +0 -2350
  485. package/deps/blake3/c/blake3_sse41.c +0 -560
  486. package/deps/blake3/c/blake3_sse41_x86-64_unix.S +0 -2028
  487. package/deps/blake3/c/blake3_sse41_x86-64_windows_gnu.S +0 -2069
  488. package/deps/blake3/c/blake3_sse41_x86-64_windows_msvc.asm +0 -2089
  489. package/deps/blake3/c/blake3_tbb.cpp +0 -37
  490. package/deps/blake3/c/dependencies/CMakeLists.txt +0 -3
  491. package/deps/blake3/c/dependencies/tbb/CMakeLists.txt +0 -28
  492. package/deps/blake3/c/example.c +0 -36
  493. package/deps/blake3/c/example_tbb.c +0 -57
  494. package/deps/blake3/c/libblake3.pc.in +0 -12
  495. package/deps/blake3/c/main.c +0 -166
  496. package/deps/blake3/c/test.py +0 -97
  497. package/deps/blake3/media/B3.svg +0 -70
  498. package/deps/blake3/media/BLAKE3.svg +0 -85
  499. package/deps/blake3/media/speed.svg +0 -1474
  500. package/deps/blake3/reference_impl/Cargo.toml +0 -8
  501. package/deps/blake3/reference_impl/README.md +0 -14
  502. package/deps/blake3/reference_impl/reference_impl.rs +0 -374
  503. package/deps/blake3/src/ffi_avx2.rs +0 -65
  504. package/deps/blake3/src/ffi_avx512.rs +0 -169
  505. package/deps/blake3/src/ffi_neon.rs +0 -82
  506. package/deps/blake3/src/ffi_sse2.rs +0 -126
  507. package/deps/blake3/src/ffi_sse41.rs +0 -126
  508. package/deps/blake3/src/guts.rs +0 -60
  509. package/deps/blake3/src/hazmat.rs +0 -704
  510. package/deps/blake3/src/io.rs +0 -64
  511. package/deps/blake3/src/join.rs +0 -92
  512. package/deps/blake3/src/lib.rs +0 -1835
  513. package/deps/blake3/src/platform.rs +0 -587
  514. package/deps/blake3/src/portable.rs +0 -198
  515. package/deps/blake3/src/rust_avx2.rs +0 -474
  516. package/deps/blake3/src/rust_sse2.rs +0 -775
  517. package/deps/blake3/src/rust_sse41.rs +0 -766
  518. package/deps/blake3/src/test.rs +0 -1049
  519. package/deps/blake3/src/traits.rs +0 -227
  520. package/deps/blake3/src/wasm32_simd.rs +0 -794
  521. package/deps/blake3/test_vectors/Cargo.toml +0 -19
  522. package/deps/blake3/test_vectors/cross_test.sh +0 -25
  523. package/deps/blake3/test_vectors/src/bin/generate.rs +0 -4
  524. package/deps/blake3/test_vectors/src/lib.rs +0 -350
  525. package/deps/blake3/test_vectors/test_vectors.json +0 -217
  526. package/deps/blake3/tools/compiler_version/Cargo.toml +0 -7
  527. package/deps/blake3/tools/compiler_version/build.rs +0 -6
  528. package/deps/blake3/tools/compiler_version/src/main.rs +0 -27
  529. package/deps/blake3/tools/instruction_set_support/Cargo.toml +0 -6
  530. package/deps/blake3/tools/instruction_set_support/src/main.rs +0 -10
  531. package/deps/blake3/tools/release.md +0 -16
  532. package/deps/ncrypto/.bazelignore +0 -4
  533. package/deps/ncrypto/.bazelrc +0 -1
  534. package/deps/ncrypto/.bazelversion +0 -1
  535. package/deps/ncrypto/.clang-format +0 -111
  536. package/deps/ncrypto/.github/workflows/bazel.yml +0 -58
  537. package/deps/ncrypto/.github/workflows/commitlint.yml +0 -16
  538. package/deps/ncrypto/.github/workflows/linter.yml +0 -38
  539. package/deps/ncrypto/.github/workflows/macos.yml +0 -43
  540. package/deps/ncrypto/.github/workflows/release-please.yml +0 -16
  541. package/deps/ncrypto/.github/workflows/ubuntu.yml +0 -128
  542. package/deps/ncrypto/.github/workflows/visual-studio.yml +0 -49
  543. package/deps/ncrypto/.python-version +0 -1
  544. package/deps/ncrypto/.release-please-manifest.json +0 -3
  545. package/deps/ncrypto/BUILD.bazel +0 -44
  546. package/deps/ncrypto/CHANGELOG.md +0 -37
  547. package/deps/ncrypto/CMakeLists.txt +0 -79
  548. package/deps/ncrypto/MODULE.bazel +0 -16
  549. package/deps/ncrypto/MODULE.bazel.lock +0 -461
  550. package/deps/ncrypto/cmake/CPM.cmake +0 -1225
  551. package/deps/ncrypto/cmake/ncrypto-flags.cmake +0 -17
  552. package/deps/ncrypto/ncrypto.pc.in +0 -10
  553. package/deps/ncrypto/patches/0001-Expose-libdecrepit-so-NodeJS-can-use-it-for-ncrypto.patch +0 -28
  554. package/deps/ncrypto/pyproject.toml +0 -38
  555. package/deps/ncrypto/release-please-config.json +0 -11
  556. package/deps/ncrypto/src/CMakeLists.txt +0 -40
  557. package/deps/ncrypto/tests/BUILD.bazel +0 -11
  558. package/deps/ncrypto/tests/CMakeLists.txt +0 -7
  559. package/deps/ncrypto/tests/basic.cpp +0 -856
  560. package/deps/ncrypto/tools/run-clang-format.sh +0 -42
  561. package/lib/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,2782 @@
1
+ # simdutf: Text processing at billions of characters per second
2
+
3
+ <!-- if(github) { (this is used by the github pages export, don't modify!) -->
4
+
5
+ [![Alpine Linux](https://github.com/simdutf/simdutf/actions/workflows/alpine.yml/badge.svg)](https://github.com/simdutf/simdutf/actions/workflows/alpine.yml) [![MSYS2-CLANG-CI](https://github.com/simdutf/simdutf/actions/workflows/msys2-clang.yml/badge.svg)](https://github.com/simdutf/simdutf/actions/workflows/msys2-clang.yml) [![Ubuntu 22.04 Sanitized CI (GCC 12, CXX 20)](https://github.com/simdutf/simdutf/actions/workflows/ubuntu22-cxx20.yml/badge.svg)](https://github.com/simdutf/simdutf/actions/workflows/ubuntu22-cxx20.yml)
6
+
7
+ <img src="doc/logo.svg" width="25%" style="float: right">
8
+
9
+ - [Real-World Usage](#real-world-usage)
10
+ - [How fast is it?](#how-fast-is-it)
11
+ - [Requirements](#requirements)
12
+ - [Usage (Usage)](#usage-usage)
13
+ - [Usage (CMake)](#usage-cmake)
14
+ - [Single-header version](#single-header-version)
15
+ - [Single-header version with limited features](#single-header-version-with-limited-features)
16
+ - [Packages](#packages)
17
+ - [Example](#example)
18
+ - [API](#api)
19
+ - [Base64](#base64)
20
+ - [Find](#find)
21
+ - [C++20 and std::span usage in simdutf](#c20-and-stdspan-usage-in-simdutf)
22
+ - [C++23 and constexpr support](#c23-and-constexpr-support)
23
+ - [The sutf command-line tool](#the-sutf-command-line-tool)
24
+ - [Manual implementation selection](#manual-implementation-selection)
25
+ - [Thread safety](#thread-safety)
26
+ - [References](#references)
27
+ - [License](#license)
28
+
29
+ <!-- } (this is used by the github pages export, don't modify!) -->
30
+
31
+ <!-- base64 KioqUGxlYXNlIHZpc2l0IGh0dHBzOi8vZ2l0aHViLmNvbS9zaW1kdXRmL3NpbWR1dGYgZm9yIHNvdXJjZSBjb2RlIGFuZCBpc3N1ZSB0cmFja2luZyEqKioK (this is used by the github pages export, don't modify!) -->
32
+
33
+ Most modern software relies on the [Unicode standard](https://en.wikipedia.org/wiki/Unicode).
34
+ In memory, Unicode strings are represented using either
35
+ UTF-8 or UTF-16. The UTF-8 format is the de facto standard on the web (JSON, HTML, etc.) and it has been adopted as the default in many popular
36
+ programming languages (Go, Zig, Rust, Swift, etc.). The UTF-16 format is standard in Java, C# and in many Windows technologies.
37
+
38
+ Not all sequences of bytes are valid Unicode strings. It is unsafe to use Unicode strings in UTF-8 and UTF-16LE without first validating them. Furthermore, we often need to convert strings from one encoding to another, by a process called [transcoding](https://en.wikipedia.org/wiki/Transcoding). For security purposes, such transcoding should be validating: it should refuse to transcode incorrect strings.
39
+
40
+ This library provide fast Unicode functions such as
41
+
42
+ - ASCII, UTF-8, UTF-16LE/BE and UTF-32 validation, with and without error identification,
43
+ - Latin1 to UTF-8 transcoding,
44
+ - Latin1 to UTF-16LE/BE transcoding
45
+ - Latin1 to UTF-32 transcoding
46
+ - UTF-8 to Latin1 transcoding, with or without validation, with and without error identification,
47
+ - UTF-8 to UTF-16LE/BE transcoding, with or without validation, with and without error identification,
48
+ - UTF-8 to UTF-32 transcoding, with or without validation, with and without error identification,
49
+ - UTF-16LE/BE to Latin1 transcoding, with or without validation, with and without error identification,
50
+ - UTF-16LE/BE to UTF-8 transcoding, with or without validation, with and without error identification,
51
+ - UTF-32 to Latin1 transcoding, with or without validation, with and without error identification,
52
+ - UTF-32 to UTF-8 transcoding, with or without validation, with and without error identification,
53
+ - UTF-32 to UTF-16LE/BE transcoding, with or without validation, with and without error identification,
54
+ - UTF-16LE/BE to UTF-32 transcoding, with or without validation, with and without error identification,
55
+ - From an UTF-8 string, compute the size of the Latin1 equivalent string,
56
+ - From an UTF-8 string, compute the size of the UTF-16 equivalent string,
57
+ - From an UTF-8 string, compute the size of the UTF-32 equivalent string (equivalent to UTF-8 character counting),
58
+ - From an UTF-16LE/BE string, compute the size of the Latin1 equivalent string,
59
+ - From an UTF-16LE/BE string, compute the size of the UTF-8 equivalent string,
60
+ - From an UTF-32 string, compute the size of the UTF-8 or UTF-16LE equivalent string,
61
+ - From an UTF-16LE/BE string, compute the size of the UTF-32 equivalent string (equivalent to UTF-16 character counting),
62
+ - UTF-8 and UTF-16LE/BE character counting,
63
+ - UTF-16 endianness change (UTF16-LE/BE to UTF-16-BE/LE),
64
+ - [WHATWG forgiving-base64](https://infra.spec.whatwg.org/#forgiving-base64-decode) (with or without URL encoding) to binary,
65
+ - Binary to base64 (with or without URL encoding).
66
+
67
+ The functions are accelerated using SIMD instructions (e.g., ARM NEON, SSE, AVX, AVX-512, RISC-V Vector Extension, LoongSon, POWER, etc.). When your strings contain hundreds of characters, we can often transcode them at speeds exceeding a billion characters per second. You should expect high speeds not only with English strings (ASCII) but also Chinese, Japanese, Arabic, and so forth. We handle the full character range (including, for example, emojis).
68
+
69
+ The library compiles down to a small library of a few hundred kilobytes. Our functions are exception-free and non allocating. We have extensive tests and extensive benchmarks.
70
+
71
+ We have exhaustive tests, including an elaborate fuzzing setup. The library has been used in production systems for years.
72
+
73
+ If using C++23 or newer, there is experimental support for using the library at compile time (constexpr).
74
+
75
+ ## Real-World Usage
76
+
77
+ The simdutf library is used by:
78
+ - [Node.js](https://nodejs.org/en/) (19.4.0 or better, 20.0 or better, 18.15 or better), a standard JavaScript runtime environment,
79
+ - [Bun](https://bun.sh), a fast JavaScript runtime environment,
80
+ - [WebKit](https://github.com/WebKit/WebKit/pull/9990), the Web engine behind the Safari browser (iOS, macOS),
81
+ - [Chromium](https://chromium-review.googlesource.com/c/chromium/src/+/6054817), the Web engine behind the Google Chrome, Microsoft Edge and Brave,
82
+ - [StarRocks](https://www.starrocks.io), an Open-Source, High-Performance Analytical Database,
83
+ - [Oracle GraalVM JavaScript](https://github.com/oracle/graaljs), a JavaScript implementation by Oracle,
84
+ - [Couchbase](https://www.couchbase.com), a popular database system,
85
+ - [Ladybird](https://ladybird.org), an independent Web browser,
86
+ - [Cloudflare workerd](https://github.com/cloudflare/workerd), a JavaScript/Wasm Runtime,
87
+ - [haskell/text](https://github.com/haskell/text), a library for fast operations over Unicode text,
88
+ - [klogg](https://github.com/variar/klogg), a Really fast log explorer,
89
+ - [Pixie](https://github.com/pixie-io/pixie), observability tool for Kubernetes applications,
90
+ - [vte](https://gitlab.gnome.org/GNOME/vte) (0.81.0 or better), a virtual terminal widget for GTK applications.
91
+
92
+
93
+
94
+ ## How fast is it?
95
+
96
+ The adoption of the simdutf library by the popular Node.js JavaScript runtime lead to a significant
97
+ performance gain:
98
+
99
+ > Decoding and Encoding becomes considerably faster than in Node.js 18. With the addition of simdutf for UTF-8 parsing the observed benchmark, results improved by 364% (an extremely impressive leap) when decoding in comparison to Node.js 16. ([State of Node.js Performance 2023](https://blog.rafaelgss.dev/state-of-nodejs-performance-2023))
100
+
101
+
102
+ <img src="doc/node2023.png" width="70%" />
103
+
104
+
105
+ Over a wide range of realistic data sources, the simdutf library transcodes a billion characters per second or more. Our approach can be 3 to 10 times faster than the popular ICU library on difficult (non-ASCII) strings. We can be 20x faster than ICU when processing easy strings (ASCII). Our good results apply to both recent x64 and ARM processors.
106
+
107
+
108
+ To illustrate, we present a benchmark result with values are in billions of characters processed by second. Consider the following figures.
109
+
110
+
111
+
112
+ <img src="doc/utf8utf16.png" width="70%" />
113
+
114
+ <img src="doc/utf16utf8.png" width="70%" />
115
+
116
+ If your system supports AVX-512, the simdutf library can provide very high performance. We get the following speed results on an Ice Lake Intel processor (both AVX2 and AVX-512) are simdutf kernels:
117
+
118
+ <img src="doc/avx512.png" width="70%" />
119
+
120
+
121
+ Datasets: https://github.com/lemire/unicode_lipsum
122
+
123
+ Please refer to our benchmarking tool for a proper interpretation of the numbers. Our results are reproducible.
124
+
125
+ ## Requirements
126
+
127
+ - C++11 compatible compiler. We support LLVM clang, GCC, Visual Studio. (Our tests and benchmark tools requires C++17.) Be aware that GCC under Windows is buggy and thus unsupported.
128
+ - For high speed, you should have a recent 64-bit system (e.g., ARM, x64, RISC-V with vector extensions, Loongson, POWER). On Loongson processors, LASX runtime dispatching is only enabled on GCC 15+, not on LLVM or older versions of GCC.
129
+ - If you rely on CMake, you should use a recent CMake (at least 3.15); otherwise you may use the [single header version](#single-header-version). The library is also available from [Microsoft's vcpkg](https://github.com/simdutf/simdutf-vcpkg), from [conan](https://conan.io/center/recipes/simdutf), from [FreeBSD's port](https://cgit.freebsd.org/ports/tree/converters/simdutf), from [brew](https://formulae.brew.sh/formula/simdutf), and many other systems.
130
+ - AVX-512 support require a processor with AVX512-VBMI2 (Ice Lake or better, AMD Zen 4 or better) and a recent compiler (GCC 8 or better, Visual Studio 2022 or better, LLVM clang 6 or better). You need a correspondingly recent assembler such as gas (2.30+) or nasm (2.14+): recent compilers usually come with recent assemblers. If you mix a recent compiler with an incompatible/old assembler (e.g., when using a recent compiler with an old Linux distribution), you may get errors at build time because the compiler produces instructions that the assembler does not recognize: you should update your assembler to match your compiler (e.g., upgrade binutils to version 2.30 or better under Linux) or use an older compiler matching the capabilities of your assembler.
131
+ - To benefit from RISC-V Vector Extensions on RISC-V systems, you should compile specifically for the desired architecture. E.g., add `-march=rv64gcv` as a compiler flag when using a version of GCC or LLVM which supports these extensions (such as GCC 14 or better). The command `CXXFLAGS=-march=rv64gcv cmake -B build` may suffice.
132
+ - We recommend that Visual Studio users compile with LLVM (ClangCL). Using LLVM as a front-end inside Visual Studio provides faster release builds and better runtime performance.
133
+
134
+ ## Usage (Usage)
135
+
136
+ We made a video to help you get started with the library.
137
+
138
+ [![the simdutf library](http://img.youtube.com/vi/H9NZtb7ykYs/0.jpg)](https://www.youtube.com/watch?v=H9NZtb7ykYs)<br />
139
+
140
+
141
+ ### Quick Start
142
+
143
+ Linux or macOS users might follow the following instructions if they have a recent C++ compiler installed and the standard utilities (`wget`, `unzip`, etc.)
144
+
145
+
146
+ 1. Pull the library in a directory
147
+ ```
148
+ wget https://github.com/simdutf/simdutf/releases/download/v8.2.0/singleheader.zip
149
+ unzip singleheader.zip
150
+ ```
151
+ You can replace `wget` by `curl -OL https://...` if you prefer.
152
+ 2. Compile
153
+ ```shell
154
+ c++ -std=c++17 -o amalgamation_demo amalgamation_demo.cpp
155
+ ```
156
+ 3. `./amalgamation_demo`
157
+
158
+ ```
159
+ valid UTF-8
160
+ wrote 4 UTF-16LE words.
161
+ valid UTF-16LE
162
+ wrote 4 UTF-8 words.
163
+ 1234
164
+ perfect round trip
165
+ ```
166
+
167
+
168
+ *We strongly discourage working from our main git branch. You should never use our main branch
169
+ in production. [Use our releases](https://github.com/simdutf/simdutf/releases/). They are tagged as `vX.Y.Z`.*
170
+
171
+ ### Usage (CMake)
172
+
173
+ ```shell
174
+ cmake -B build
175
+ cmake --build build
176
+ cd build
177
+ ctest .
178
+ ```
179
+
180
+ Visual Studio users must specify whether they want to build the Release or Debug version.
181
+
182
+ To use the library as a CMake dependency in your project, please see `tests/installation_tests/from_fetch` for
183
+ an example.
184
+
185
+ You may also use a package manager. E.g., [we have a complete example using vcpkg](https://github.com/simdutf/simdutf-vcpkg).
186
+
187
+
188
+
189
+ ## Single-header version
190
+
191
+ You can create a single-header version of the library where
192
+ all of the code is put into two files (`simdutf.h` and `simdutf.cpp`).
193
+ We publish a zip archive containing these files, e.g., see
194
+ https://github.com/simdutf/simdutf/releases/download/v8.2.0/singleheader.zip
195
+
196
+ You may generate it on your own using a Python script.
197
+
198
+ ```shell
199
+ python3 ./singleheader/amalgamate.py
200
+ ```
201
+
202
+ We require Python 3 or better.
203
+
204
+ Under Linux and macOS, you may test it as follows:
205
+
206
+ ```shell
207
+ cd singleheader
208
+ c++ -o amalgamation_demo amalgamation_demo.cpp -std=c++17
209
+ ./amalgamation_demo
210
+ ```
211
+
212
+ ### Single-header version with limited features
213
+
214
+ When creating a single-header version, it is possible to limit which
215
+ features are enabled. Then the API of library is limited too and the
216
+ amalgamated sources do not include code related to disabled features.
217
+
218
+ The script `singleheader/amalgamate.py` accepts the following parameters:
219
+
220
+ * `--with-utf8` - procedures related only to UTF-8 encoding (like string validation);
221
+ * `--with-utf16` - likewise: only UTF-16 encoding;
222
+ * `--with-utf32` - likewise: only UTF-32 encoding;
223
+ * `--with-ascii` - procedures related to ASCII encoding;
224
+ * `--with-latin1` - convert between selected UTF encodings and Latin1;
225
+ * `--with-base64` - procedures related to Base64 encoding, includes 'find';
226
+ * `--with-detect-enc` - enable detect encoding.
227
+
228
+ If we need conversion between different encodings, like UTF-8 and UTF-32, then
229
+ these two features have to be enabled.
230
+
231
+ The amalgamated sources set to 1 the following preprocessor defines:
232
+
233
+ * `SIMDUTF_FEATURE_UTF8`,
234
+ * `SIMDUTF_FEATURE_UTF16`,
235
+ * `SIMDUTF_FEATURE_UTF32`,
236
+ * `SIMDUTF_FEATURE_ASCII`,
237
+ * `SIMDUTF_FEATURE_LATIN1`,
238
+ * `SIMDUTF_FEATURE_BASE64`,
239
+ * `SIMDUTF_FEATURE_DETECT_ENCODING`.
240
+
241
+ Thus, when it is needed to make sure the correct set of features are
242
+ enabled, we may test it using preprocessor:
243
+
244
+ ```cpp
245
+ #if SIMDUTF_FEATURE_UTF16 || SIMDUTF_FEATURE_UTF32
246
+ #error "Please amalagamate simdutf without UTF-16 and UTF-32"
247
+ #endif
248
+ ```
249
+
250
+
251
+ ### Packages
252
+ ------
253
+
254
+ [![Packaging status](https://repology.org/badge/vertical-allrepos/simdutf.svg)](https://repology.org/project/simdutf/versions)
255
+
256
+
257
+ ### Example
258
+
259
+ Using the single-header version, you could compile the following program.
260
+
261
+ ```cpp
262
+ #include <iostream>
263
+ #include <memory>
264
+
265
+ #include "simdutf.cpp"
266
+ #include "simdutf.h"
267
+
268
+ int main(int argc, char *argv[]) {
269
+ const char *source = "1234";
270
+ // 4 == strlen(source)
271
+ bool validutf8 = simdutf::validate_utf8(source, 4);
272
+ if (validutf8) {
273
+ std::cout << "valid UTF-8" << std::endl;
274
+ } else {
275
+ std::cerr << "invalid UTF-8" << std::endl;
276
+ return EXIT_FAILURE;
277
+ }
278
+ // We need a buffer of size where to write the UTF-16LE code units.
279
+ size_t expected_utf16words = simdutf::utf16_length_from_utf8(source, 4);
280
+ std::unique_ptr<char16_t[]> utf16_output{new char16_t[expected_utf16words]};
281
+ // convert to UTF-16LE
282
+ size_t utf16words =
283
+ simdutf::convert_utf8_to_utf16le(source, 4, utf16_output.get());
284
+ std::cout << "wrote " << utf16words << " UTF-16LE code units." << std::endl;
285
+ // It wrote utf16words * sizeof(char16_t) bytes.
286
+ bool validutf16 = simdutf::validate_utf16le(utf16_output.get(), utf16words);
287
+ if (validutf16) {
288
+ std::cout << "valid UTF-16LE" << std::endl;
289
+ } else {
290
+ std::cerr << "invalid UTF-16LE" << std::endl;
291
+ return EXIT_FAILURE;
292
+ }
293
+ // convert it back:
294
+ // We need a buffer of size where to write the UTF-8 code units.
295
+ size_t expected_utf8words =
296
+ simdutf::utf8_length_from_utf16le(utf16_output.get(), utf16words);
297
+ std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
298
+ // convert to UTF-8
299
+ size_t utf8words = simdutf::convert_utf16le_to_utf8(
300
+ utf16_output.get(), utf16words, utf8_output.get());
301
+ std::cout << "wrote " << utf8words << " UTF-8 code units." << std::endl;
302
+ std::string final_string(utf8_output.get(), utf8words);
303
+ std::cout << final_string << std::endl;
304
+ if (final_string != source) {
305
+ std::cerr << "bad conversion" << std::endl;
306
+ return EXIT_FAILURE;
307
+ } else {
308
+ std::cerr << "perfect round trip" << std::endl;
309
+ }
310
+ return EXIT_SUCCESS;
311
+ }
312
+ ```
313
+
314
+ ## API
315
+
316
+ Our API is made of a few non-allocating functions. They typically take a pointer and a length as a parameter,
317
+ and they sometimes take a pointer to an output buffer. Users are responsible for memory allocation.
318
+
319
+ We use three types of data pointer types:
320
+ - `char*` for UTF-8 or indeterminate Unicode formats,
321
+ - `char16_t*` for UTF-16 (both UTF-16LE and UTF-16BE),
322
+ - `char32_t*` for UTF-32. UTF-32 is primarily used for internal use, not data interchange. Thus, unless otherwise stated, `char32_t` refers to the native type and is typically UTF-32LE since virtually all systems are little-endian today.
323
+ In generic terms, we refer to `char`, `char16_t`, and `char32_t` as *code units*. A *character* may use several *code units*: between 1 and 4 code units in UTF-8, and between
324
+ 1 and 2 code units in UTF-16LE and UTF-16BE.
325
+
326
+ Our functions and declarations are all in the `simdutf` namespace. Thus you should prefix our functions
327
+ and types with `simdutf::` as required.
328
+
329
+ If using C++20, all functions which take a pointer and a size (which is almost all of them)
330
+ also have a span overload. Here is an example:
331
+
332
+ ```cpp
333
+ std::vector<char> data{1, 2, 3, 4, 5};
334
+ // C++11 API
335
+ auto cpp11 = simdutf::autodetect_encoding(data.data(), data.size());
336
+ // C++20 API
337
+ auto cpp20 = simdutf::autodetect_encoding(data);
338
+ ```
339
+
340
+ The span overloads use std::span for UTF-16 and UTF-32. For latin1, UTF-8,
341
+ "binary" (used by the base64 functions) anything that has a `.size()` and
342
+ `.data()` that returns a pointer to a byte-like type will be accepted as a
343
+ span. This makes it possible to directly pass std::string, std::string_view,
344
+ std::vector, std::array and std::span to the functions. The reason for allowing
345
+ all byte-like types in the api (as opposed to only `std::span<char>`) is to
346
+ make it easy to interface with whatever data the user may have, without having
347
+ to resort to casting.
348
+
349
+ We have basic functions to detect the type of an input. They return an integer defined by
350
+ the following `enum`.
351
+
352
+ ```cpp
353
+ enum encoding_type {
354
+ UTF8 = 1, // BOM 0xef 0xbb 0xbf
355
+ UTF16_LE = 2, // BOM 0xff 0xfe
356
+ UTF16_BE = 4, // BOM 0xfe 0xff
357
+ UTF32_LE = 8, // BOM 0xff 0xfe 0x00 0x00
358
+ UTF32_BE = 16, // BOM 0x00 0x00 0xfe 0xff
359
+
360
+ unspecified = 0
361
+ };
362
+ ```
363
+
364
+ ```cpp
365
+
366
+ /**
367
+ * Autodetect the encoding of the input, a single encoding is recommended.
368
+ * E.g., the function might return simdutf::encoding_type::UTF8,
369
+ * simdutf::encoding_type::UTF16_LE, simdutf::encoding_type::UTF16_BE, or
370
+ * simdutf::encoding_type::UTF32_LE.
371
+ *
372
+ * @param input the string to analyze.
373
+ * @param length the length of the string in bytes.
374
+ * @return the detected encoding type
375
+ */
376
+ simdutf_warn_unused simdutf::encoding_type autodetect_encoding(const char *input, size_t length) noexcept;
377
+
378
+ /**
379
+ * Autodetect the possible encodings of the input in one pass.
380
+ * E.g., if the input might be UTF-16LE or UTF-8, this function returns
381
+ * the value (simdutf::encoding_type::UTF8 | simdutf::encoding_type::UTF16_LE).
382
+ *
383
+ * @param input the string to analyze.
384
+ * @param length the length of the string in bytes.
385
+ * @return the detected encoding type
386
+ */
387
+ simdutf_warn_unused int detect_encodings(const char *input, size_t length) noexcept;
388
+ ```
389
+
390
+
391
+
392
+ For validation and transcoding, we also provide functions that will stop on error and return a result struct which is a pair of two fields:
393
+ ```cpp
394
+ struct result {
395
+ error_code error; // see `struct error_code`.
396
+ size_t count; // In case of error, indicates the position of the error in the input in code units.
397
+ // In case of success, indicates the number of code units validated/written.
398
+ };
399
+ ```
400
+ On error, the `error` field indicates the type of error encountered and the `count` field indicates the position of the error in the input in code units or the number of characters validated/written.
401
+ We report six types of errors related to Latin1, UTF-8, UTF-16 and UTF-32 encodings:
402
+ ```cpp
403
+ enum error_code {
404
+ SUCCESS = 0,
405
+ HEADER_BITS, // Any byte must have fewer than 5 header bits.
406
+ TOO_SHORT, // The leading byte must be followed by N-1 continuation bytes,
407
+ // where N is the UTF-8 character length This is also the error
408
+ // when the input is truncated.
409
+ TOO_LONG, // We either have too many consecutive continuation bytes or the
410
+ // string starts with a continuation byte.
411
+ OVERLONG, // The decoded character must be above U+7F for two-byte characters,
412
+ // U+7FF for three-byte characters, and U+FFFF for four-byte
413
+ // characters.
414
+ TOO_LARGE, // The decoded character must be less than or equal to
415
+ // U+10FFFF,less than or equal than U+7F for ASCII OR less than
416
+ // equal than U+FF for Latin1
417
+ SURROGATE, // The decoded character must be not be in U+D800...DFFF (UTF-8 or
418
+ // UTF-32)
419
+ // OR
420
+ // a high surrogate must be followed by a low surrogate
421
+ // and a low surrogate must be preceded by a high surrogate
422
+ // (UTF-16)
423
+ // OR
424
+ // there must be no surrogate at all and one is
425
+ // found (Latin1 functions)
426
+ // OR
427
+ // *specifically* for the function
428
+ // utf8_length_from_utf16_with_replacement, a surrogate (whether
429
+ // in error or not) has been found (I.e., whether we are in the
430
+ // Basic Multilingual Plane or not).
431
+ INVALID_BASE64_CHARACTER, // Found a character that cannot be part of a valid
432
+ // base64 string. This may include a misplaced padding character ('=').
433
+ BASE64_INPUT_REMAINDER, // The base64 input terminates with a single
434
+ // character, excluding padding (=). It is also used
435
+ // in strict mode when padding is not adequate.
436
+ BASE64_EXTRA_BITS, // The base64 input terminates with non-zero
437
+ // padding bits.
438
+ OUTPUT_BUFFER_TOO_SMALL, // The provided buffer is too small.
439
+ OTHER // Not related to validation/transcoding.
440
+ };
441
+
442
+ ```
443
+ On success, the `error` field is set to `SUCCESS` and the `position` field indicates either the number of code units validated for validation functions or the number of written
444
+ code units in the output format for transcoding functions. In ASCII, Latin1 and UTF-8, code units occupy 8 bits (they are bytes); in UTF-16LE and UTF-16BE, code units occupy 16 bits; in UTF-32, code units occupy 32 bits.
445
+
446
+ Generally speaking, functions that report errors always stop soon after an error is
447
+ encountered and might therefore be faster on inputs where an error occurs early in the input.
448
+ The functions that return a boolean indicating whether or not an error has been encountered
449
+ are meant to be used in an *optimistic setting*---when we expect that inputs will almost always
450
+ be correct.
451
+
452
+ You may use functions that report an error to indicate where the problem happens during, as follows:
453
+
454
+ ```cpp
455
+ std::string bad_ascii = "\x20\x20\x20\x20\x20\xff\x20\x20\x20";
456
+ simdutf::result res = simdtuf::validate_ascii_with_errors(bad_ascii.data(), bad_ascii.size());
457
+ if(res.error != simdutf::error_code::SUCCESS) {
458
+ std::cerr << "error at index " << res.count << std::endl;
459
+ }
460
+ ```
461
+
462
+ Or as follows:
463
+
464
+ ```cpp
465
+ std::string bad_utf8 = "\xc3\xa9\xc3\xa9\x20\xff\xc3\xa9";
466
+ simdutf::result res = simdtuf::validate_utf8_with_errors(bad_utf8.data(), bad_utf8.size());
467
+ if(res.error != simdutf::error_code::SUCCESS) {
468
+ std::cerr << "error at index " << res.count << std::endl;
469
+ }
470
+ res = simdtuf::validate_utf8_with_errors(bad_utf8.data(), res.count);
471
+ // will be successful in this case
472
+ if(res.error == simdutf::error_code::SUCCESS) {
473
+ std::cerr << "we have " << res.count << "valid bytes" << std::endl;
474
+ }
475
+ ```
476
+
477
+
478
+ We have fast validation functions.
479
+
480
+ ```cpp
481
+ /**
482
+ * Validate the ASCII string.
483
+ *
484
+ * @param buf the ASCII string to validate.
485
+ * @param len the length of the string in bytes.
486
+ * @return true if and only if the string is valid ASCII.
487
+ */
488
+ simdutf_warn_unused bool validate_ascii(const char *buf, size_t len) noexcept;
489
+
490
+ /**
491
+ * Validate the ASCII string and stop on error.
492
+ *
493
+ * @param buf the ASCII string to validate.
494
+ * @param len the length of the string in bytes.
495
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
496
+ */
497
+ simdutf_warn_unused result validate_ascii_with_errors(const char *buf, size_t len) noexcept;
498
+
499
+ /**
500
+ * Validate the ASCII string as a UTF-16 sequence.
501
+ * An UTF-16 sequence is considered an ASCII sequence
502
+ * if it could be converted to an ASCII string losslessly.
503
+ *
504
+ * @param buf the UTF-16 string to validate.
505
+ * @param len the length of the string in bytes.
506
+ * @return true if and only if the string is valid ASCII.
507
+ */
508
+ simdutf_warn_unused bool validate_utf16_as_ascii(const char16_t *buf,
509
+ size_t len) noexcept;
510
+
511
+ /**
512
+ * Validate the ASCII string as a UTF-16BE sequence.
513
+ * An UTF-16 sequence is considered an ASCII sequence
514
+ * if it could be converted to an ASCII string losslessly.
515
+ *
516
+ * @param buf the UTF-16BE string to validate.
517
+ * @param len the length of the string in bytes.
518
+ * @return true if and only if the string is valid ASCII.
519
+ */
520
+ simdutf_warn_unused bool validate_utf16be_as_ascii(const char16_t *buf,
521
+ size_t len) noexcept;
522
+ /**
523
+ * Validate the ASCII string as a UTF-16LE sequence.
524
+ * An UTF-16 sequence is considered an ASCII sequence
525
+ * if it could be converted to an ASCII string losslessly.
526
+ *
527
+ * @param buf the UTF-16LE string to validate.
528
+ * @param len the length of the string in bytes.
529
+ * @return true if and only if the string is valid ASCII.
530
+ */
531
+ simdutf_warn_unused bool validate_utf16le_as_ascii(const char16_t *buf,
532
+ size_t len) noexcept;
533
+
534
+ /**
535
+ * Validate the UTF-8 string. This function may be best when you expect
536
+ * the input to be almost always valid. Otherwise, consider using
537
+ * validate_utf8_with_errors.
538
+ *
539
+ * @param buf the UTF-8 string to validate.
540
+ * @param len the length of the string in bytes.
541
+ * @return true if and only if the string is valid UTF-8.
542
+ */
543
+ simdutf_warn_unused bool validate_utf8(const char *buf, size_t len) noexcept;
544
+
545
+ /**
546
+ * Validate the UTF-8 string and stop on error. It might be faster than
547
+ * validate_utf8 when an error is expected to occur early.
548
+ *
549
+ * @param buf the UTF-8 string to validate.
550
+ * @param len the length of the string in bytes.
551
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
552
+ */
553
+ simdutf_warn_unused result validate_utf8_with_errors(const char *buf, size_t len) noexcept;
554
+
555
+ /**
556
+ * Using native endianness; Validate the UTF-16 string.
557
+ * This function may be best when you expect the input to be almost always valid.
558
+ * Otherwise, consider using validate_utf16_with_errors.
559
+ *
560
+ * This function is not BOM-aware.
561
+ *
562
+ * @param buf the UTF-16 string to validate.
563
+ * @param len the length of the string in number of 2-byte code units (char16_t).
564
+ * @return true if and only if the string is valid UTF-16.
565
+ */
566
+ simdutf_warn_unused bool validate_utf16(const char16_t *buf, size_t len) noexcept;
567
+
568
+ /**
569
+ * Validate the UTF-16LE string. This function may be best when you expect
570
+ * the input to be almost always valid. Otherwise, consider using
571
+ * validate_utf16le_with_errors.
572
+ *
573
+ * This function is not BOM-aware.
574
+ *
575
+ * @param buf the UTF-16LE string to validate.
576
+ * @param len the length of the string in number of 2-byte code units (char16_t).
577
+ * @return true if and only if the string is valid UTF-16LE.
578
+ */
579
+ simdutf_warn_unused bool validate_utf16le(const char16_t *buf, size_t len) noexcept;
580
+
581
+ /**
582
+ * Validate the UTF-16BE string. This function may be best when you expect
583
+ * the input to be almost always valid. Otherwise, consider using
584
+ * validate_utf16be_with_errors.
585
+ *
586
+ * This function is not BOM-aware.
587
+ *
588
+ * @param buf the UTF-16BE string to validate.
589
+ * @param len the length of the string in number of 2-byte code units (char16_t).
590
+ * @return true if and only if the string is valid UTF-16BE.
591
+ */
592
+ simdutf_warn_unused bool validate_utf16be(const char16_t *buf, size_t len) noexcept;
593
+
594
+ /**
595
+ * Using native endianness; Validate the UTF-16 string and stop on error.
596
+ * It might be faster than validate_utf16 when an error is expected to occur early.
597
+ *
598
+ * This function is not BOM-aware.
599
+ *
600
+ * @param buf the UTF-16 string to validate.
601
+ * @param len the length of the string in number of 2-byte code units (char16_t).
602
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
603
+ */
604
+ simdutf_warn_unused result validate_utf16_with_errors(const char16_t *buf, size_t len) noexcept;
605
+
606
+ /**
607
+ * Validate the UTF-16LE string and stop on error. It might be faster than
608
+ * validate_utf16le when an error is expected to occur early.
609
+ *
610
+ * This function is not BOM-aware.
611
+ *
612
+ * @param buf the UTF-16LE string to validate.
613
+ * @param len the length of the string in number of 2-byte code units (char16_t).
614
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
615
+ */
616
+ simdutf_warn_unused result validate_utf16le_with_errors(const char16_t *buf, size_t len) noexcept;
617
+
618
+ /**
619
+ * Validate the UTF-16BE string and stop on error. It might be faster than
620
+ * validate_utf16be when an error is expected to occur early.
621
+ *
622
+ * This function is not BOM-aware.
623
+ *
624
+ * @param buf the UTF-16BE string to validate.
625
+ * @param len the length of the string in number of 2-byte code units (char16_t).
626
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
627
+ */
628
+ simdutf_warn_unused result validate_utf16be_with_errors(const char16_t *buf, size_t len) noexcept;
629
+
630
+ /**
631
+ * Validate the UTF-32 string.
632
+ *
633
+ * This function is not BOM-aware.
634
+ *
635
+ * @param buf the UTF-32 string to validate.
636
+ * @param len the length of the string in number of 4-byte code units (char32_t).
637
+ * @return true if and only if the string is valid UTF-32.
638
+ */
639
+ simdutf_warn_unused bool validate_utf32(const char32_t *buf, size_t len) noexcept;
640
+
641
+ /**
642
+ * Validate the UTF-32 string and stop on error.
643
+ *
644
+ * This function is not BOM-aware.
645
+ *
646
+ * @param buf the UTF-32 string to validate.
647
+ * @param len the length of the string in number of 4-byte code units (char32_t).
648
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
649
+ */
650
+ simdutf_warn_unused result validate_utf32_with_errors(const char32_t *buf, size_t len) noexcept;
651
+
652
+ ```
653
+
654
+ Given a potentially invalid UTF-16 input, you may want to make it correct, by using
655
+ a replacement character whenever needed. We have fast functions for this purpose
656
+ (`to_well_formed_utf16`, `to_well_formed_utf16le`, and `to_well_formed_utf16be`).
657
+ They can either copy the string while fixing it, or they can be used to fix
658
+ a string in-place.
659
+
660
+ ```cpp
661
+
662
+ /**
663
+ * Fixes an ill-formed UTF-16LE string by replacing mismatched surrogates with
664
+ * the Unicode replacement character U+FFFD. If input and output points to
665
+ * different memory areas, the procedure copies string, and it's expected that
666
+ * output memory is at least as big as the input. It's also possible to set
667
+ * input equal output, that makes replacements an in-place operation.
668
+ *
669
+ * @param input the UTF-16LE string to correct.
670
+ * @param len the length of the string in number of 2-byte code units
671
+ * (char16_t).
672
+ * @param output the output buffer.
673
+ */
674
+ void to_well_formed_utf16le(const char16_t *input, size_t len,
675
+ char16_t *output) noexcept;
676
+
677
+ /**
678
+ * Fixes an ill-formed UTF-16BE string by replacing mismatched surrogates with
679
+ * the Unicode replacement character U+FFFD. If input and output points to
680
+ * different memory areas, the procedure copies string, and it's expected that
681
+ * output memory is at least as big as the input. It's also possible to set
682
+ * input equal output, that makes replacements an in-place operation.
683
+ *
684
+ * @param input the UTF-16BE string to correct.
685
+ * @param len the length of the string in number of 2-byte code units
686
+ * (char16_t).
687
+ * @param output the output buffer.
688
+ */
689
+ void to_well_formed_utf16be(const char16_t *input, size_t len,
690
+ char16_t *output) noexcept;
691
+
692
+ /**
693
+ * Fixes an ill-formed UTF-16 string by replacing mismatched surrogates with the
694
+ * Unicode replacement character U+FFFD. If input and output points to different
695
+ * memory areas, the procedure copies string, and it's expected that output
696
+ * memory is at least as big as the input. It's also possible to set input equal
697
+ * output, that makes replacements an in-place operation.
698
+ *
699
+ * @param input the UTF-16 string to correct.
700
+ * @param len the length of the string in number of 2-byte code units
701
+ * (char16_t).
702
+ * @param output the output buffer.
703
+ */
704
+ void to_well_formed_utf16(const char16_t *input, size_t len,
705
+ char16_t *output) noexcept;
706
+ ```
707
+
708
+ Given a valid UTF-8 or UTF-16 input, you may count the number Unicode characters using
709
+ fast functions. For UTF-32, there is no need for a function given that each character
710
+ requires a flat 4 bytes. Likewise for Latin1: one byte will always equal one character.
711
+
712
+ ```cpp
713
+ /**
714
+ * Count the number of code points (characters) in the string assuming that
715
+ * it is valid.
716
+ *
717
+ * This function assumes that the input string is valid UTF-16 (native endianness).
718
+ * It is acceptable to pass invalid UTF-16 strings but in such cases
719
+ * the result is implementation defined.
720
+ *
721
+ * This function is not BOM-aware.
722
+ *
723
+ * @param input the UTF-16 string to process
724
+ * @param length the length of the string in 2-byte code units (char16_t)
725
+ * @return number of code points
726
+ */
727
+ simdutf_warn_unused size_t count_utf16(const char16_t * input, size_t length) noexcept;
728
+
729
+ /**
730
+ * Count the number of code points (characters) in the string assuming that
731
+ * it is valid.
732
+ *
733
+ * This function assumes that the input string is valid UTF-16LE.
734
+ * It is acceptable to pass invalid UTF-16 strings but in such cases
735
+ * the result is implementation defined.
736
+ *
737
+ * This function is not BOM-aware.
738
+ *
739
+ * @param input the UTF-16LE string to process
740
+ * @param length the length of the string in 2-byte code units (char16_t)
741
+ * @return number of code points
742
+ */
743
+ simdutf_warn_unused size_t count_utf16le(const char16_t * input, size_t length) noexcept;
744
+
745
+ /**
746
+ * Count the number of code points (characters) in the string assuming that
747
+ * it is valid.
748
+ *
749
+ * This function assumes that the input string is valid UTF-16BE.
750
+ * It is acceptable to pass invalid UTF-16 strings but in such cases
751
+ * the result is implementation defined.
752
+ *
753
+ * This function is not BOM-aware.
754
+ *
755
+ * @param input the UTF-16BE string to process
756
+ * @param length the length of the string in 2-byte code units (char16_t)
757
+ * @return number of code points
758
+ */
759
+ simdutf_warn_unused size_t count_utf16be(const char16_t * input, size_t length) noexcept;
760
+
761
+ /**
762
+ * Count the number of code points (characters) in the string assuming that
763
+ * it is valid.
764
+ *
765
+ * This function assumes that the input string is valid UTF-8.
766
+ * It is acceptable to pass invalid UTF-8 strings but in such cases
767
+ * the result is implementation defined.
768
+ *
769
+ * @param input the UTF-8 string to process
770
+ * @param length the length of the string in bytes
771
+ * @return number of code points
772
+ */
773
+ simdutf_warn_unused size_t count_utf8(const char * input, size_t length) noexcept;
774
+
775
+ ```
776
+
777
+ Prior to transcoding an input, you need to allocate enough memory to receive the result.
778
+ We have fast function that scan the input and compute the size of the output. These functions
779
+ are fast and non-validating.
780
+
781
+
782
+
783
+ ```cpp
784
+
785
+ /**
786
+ * Return the number of bytes that this Latin1 string would require in UTF-8 format.
787
+ *
788
+ * @param input the Latin1 string to convert
789
+ * @param length the length of the string bytes
790
+ * @return the number of bytes required to encode the Latin1 string as UTF-8
791
+ */
792
+ simdutf_warn_unused size_t utf8_length_from_latin1(const char * input, size_t length) noexcept;
793
+
794
+ /**
795
+ * Compute the number of bytes that this UTF-8 string would require in Latin1 format.
796
+ *
797
+ * This function does not validate the input. It is acceptable to pass invalid UTF-8 strings but in such cases
798
+ * the result is implementation defined.
799
+ *
800
+ * This function is not BOM-aware.
801
+ *
802
+ * @param input the UTF-8 string to convert
803
+ * @param length the length of the string in byte
804
+ * @return the number of bytes required to encode the UTF-8 string as Latin1
805
+ */
806
+ simdutf_warn_unused size_t latin1_length_from_utf8(const char * input, size_t length) noexcept;
807
+
808
+ /**
809
+ * Compute the number of bytes that this UTF-16 string would require in Latin1 format.
810
+ *
811
+ * @param length the length of the string in Latin1 code units (char)
812
+ * @return the length of the string in Latin1 code units (char) required to encode the UTF-16 string as Latin1
813
+ */
814
+ simdutf_warn_unused size_t latin1_length_from_utf16(size_t length) noexcept;
815
+
816
+ /*
817
+ * Compute the number of bytes that this UTF-16LE/BE string would require in Latin1 format.
818
+ *
819
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
820
+ * the result is implementation defined.
821
+ *
822
+ * This function is not BOM-aware.
823
+ *
824
+ * @param length the length of the string in 2-byte code units (char16_t)
825
+ * @return the number of bytes required to encode the UTF-16LE string as Latin1
826
+ */
827
+ simdutf_warn_unused size_t latin1_length_from_utf16(size_t length) noexcept;
828
+
829
+ /**
830
+ * Compute the number of bytes that this UTF-32 string would require in Latin1 format.
831
+ *
832
+ * This function does not validate the input. It is acceptable to pass invalid UTF-32 strings but in such cases
833
+ * the result is implementation defined.
834
+ *
835
+ * This function is not BOM-aware.
836
+ *
837
+ * @param length the length of the string in 4-byte code units (char32_t)
838
+ * @return the number of bytes required to encode the UTF-32 string as Latin1
839
+ */
840
+ simdutf_warn_unused size_t latin1_length_from_utf32(size_t length) noexcept;
841
+
842
+ /**
843
+ * Compute the number of 2-byte code units that this UTF-8 string would require in UTF-16 format.
844
+ *
845
+ * This function does not validate the input. It is acceptable to pass invalid UTF-8 strings but in such cases
846
+ * the result is implementation defined.
847
+ *
848
+ * @param input the UTF-8 string to process
849
+ * @param length the length of the string in bytes
850
+ * @return the number of char16_t code units required to encode the UTF-8 string as UTF-16
851
+ */
852
+ simdutf_warn_unused size_t utf16_length_from_utf8(const char * input, size_t length) noexcept;
853
+
854
+
855
+ /**
856
+ * Compute the number of 4-byte code units that this UTF-8 string would require in UTF-32 format.
857
+ *
858
+ * This function is equivalent to count_utf8
859
+ *
860
+ * This function does not validate the input. It is acceptable to pass invalid UTF-8 strings but in such cases
861
+ * the result is implementation defined.
862
+ *
863
+ * This function is not BOM-aware.
864
+ *
865
+ * @param input the UTF-8 string to process
866
+ * @param length the length of the string in bytes
867
+ * @return the number of char32_t code units required to encode the UTF-8 string as UTF-32
868
+ */
869
+ simdutf_warn_unused size_t utf32_length_from_utf8(const char * input, size_t length) noexcept;
870
+
871
+ /**
872
+ * Using native endianness; Compute the number of bytes that this UTF-16
873
+ * string would require in UTF-8 format.
874
+ *
875
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
876
+ * the result is implementation defined.
877
+ *
878
+ * @param input the UTF-16 string to convert
879
+ * @param length the length of the string in 2-byte code units (char16_t)
880
+ * @return the number of bytes required to encode the UTF-16LE string as UTF-8
881
+ */
882
+ simdutf_warn_unused size_t utf8_length_from_utf16(const char16_t * input, size_t length) noexcept;
883
+
884
+ /**
885
+ * Using native endianness; compute the number of bytes that this UTF-16
886
+ * string would require in UTF-8 format even when the UTF-16 content contains mismatched
887
+ * surrogates that have to be replaced by the replacement character (0xFFFD).
888
+ *
889
+ * @param input the UTF-16 string to convert
890
+ * @param length the length of the string in 2-byte code units (char16_t)
891
+ * @return the number of bytes required to encode the UTF-16 string as UTF-8
892
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count)
893
+ * where the count is the number of bytes required to encode the UTF-16 string as UTF-8, and the
894
+ * error code is either SUCCESS or SURROGATE. The count is correct regardless of the error field.
895
+ * When SURROGATE is returned, it does not indicate an error in the case of this function:
896
+ * it indicates that at least one surrogate has been encountered: the surrogates may be matched
897
+ * or not (thus this function does not validate). If the returned error code is SUCCESS,
898
+ * then the input contains no surrogate, is in the Basic Multilingual Plane, and is necessarily valid.
899
+ */
900
+ simdutf_warn_unused result utf8_length_from_utf16_with_replacement(const char16_t *input,
901
+ size_t length) noexcept;
902
+ /**
903
+ * Compute the number of bytes that this UTF-16LE string would require in UTF-8 format.
904
+ *
905
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
906
+ * the result is implementation defined.
907
+ *
908
+ * @param input the UTF-16LE string to convert
909
+ * @param length the length of the string in 2-byte code units (char16_t)
910
+ * @return the number of bytes required to encode the UTF-16LE string as UTF-8
911
+ */
912
+ simdutf_warn_unused size_t utf8_length_from_utf16le(const char16_t * input, size_t length) noexcept;
913
+
914
+ /**
915
+ * Compute the number of bytes that this UTF-16BE string would require in UTF-8 format.
916
+ *
917
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
918
+ * the result is implementation defined.
919
+ *
920
+ * @param input the UTF-16BE string to convert
921
+ * @param length the length of the string in 2-byte code units (char16_t)
922
+ * @return the number of bytes required to encode the UTF-16BE string as UTF-8
923
+ */
924
+ simdutf_warn_unused size_t utf8_length_from_utf16be(const char16_t * input, size_t length) noexcept;
925
+
926
+ /**
927
+ * Compute the number of bytes that this UTF-16LE string would require in UTF-8
928
+ * format even when the UTF-16LE content contains mismatched surrogates
929
+ * that have to be replaced by the replacement character (0xFFFD).
930
+ *
931
+ * @param input the UTF-16LE string to convert
932
+ * @param length the length of the string in 2-byte code units (char16_t)
933
+ * @return the number of bytes required to encode the UTF-16LE string as UTF-8
934
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count)
935
+ * where the count is the number of bytes required to encode the UTF-16LE string as UTF-8, and the
936
+ * error code is either SUCCESS or SURROGATE. The count is correct regardless of the error field.
937
+ * When SURROGATE is returned, it does not indicate an error in the case of this function:
938
+ * it indicates that at least one surrogate has been encountered: the surrogates may be matched
939
+ * or not (thus this function does not validate). If the returned error code is SUCCESS,
940
+ * then the input contains no surrogate, is in the Basic Multilingual Plane, and is necessarily valid.
941
+ */
942
+ simdutf_warn_unused result utf8_length_from_utf16le_with_replacement(
943
+ const char16_t *input, size_t length) noexcept;
944
+
945
+
946
+ /**
947
+ * Compute the number of bytes that this UTF-16BE string would require in UTF-8
948
+ * format even when the UTF-16BE content contains mismatched surrogates
949
+ * that have to be replaced by the replacement character (0xFFFD).
950
+ *
951
+ * @param input the UTF-16BE string to convert
952
+ * @param length the length of the string in 2-byte code units (char16_t)
953
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count)
954
+ * where the count is the number of bytes required to encode the UTF-16LE string as UTF-8, and
955
+ * the error code is either SUCCESS or SURROGATE. The count is correct regardless of the error field.
956
+ * When SURROGATE is returned, it does not indicate an error in the case of this function:
957
+ * it indicates that at least one surrogate has been encountered: the surrogates may be matched
958
+ * or not (thus this function does not validate). If the returned error code is SUCCESS,
959
+ * then the input contains no surrogate, is in the Basic Multilingual Plane, and is necessarily valid.
960
+ */
961
+ simdutf_warn_unused result utf8_length_from_utf16be_with_replacement(
962
+ const char16_t *input, size_t length) noexcept;
963
+
964
+ /**
965
+ * Compute the number of bytes that this UTF-16LE string would require in UTF-8
966
+ * format even when the UTF-16LE content contains mismatched surrogates
967
+ * that have to be replaced by the replacement character (0xFFFD).
968
+ *
969
+ * @param input the UTF-16LE string to convert
970
+ * @param length the length of the string in 2-byte code units (char16_t)
971
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) where the count is the number of bytes required to encode the UTF-16LE string as UTF-8, and the error code is either SUCCESS or SURROGATE. The count is correct regardless of the error field.
972
+ */
973
+ simdutf_warn_unused result utf8_length_from_utf16le_with_replacement(
974
+ const char16_t *input, size_t length) noexcept;
975
+
976
+ /**
977
+ * Compute the number of bytes that this UTF-16BE string would require in UTF-8
978
+ * format even when the UTF-16BE content contains mismatched surrogates
979
+ * that have to be replaced by the replacement character (0xFFFD).
980
+ *
981
+ * @param input the UTF-16BE string to convert
982
+ * @param length the length of the string in 2-byte code units (char16_t)
983
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) where the count is the number of bytes required to encode the UTF-16LE string as UTF-8, and the error code is either SUCCESS or SURROGATE. The count is correct regardless of the error field.
984
+ */
985
+ simdutf_warn_unused result utf8_length_from_utf16be_with_replacement(
986
+ const char16_t *input, size_t length) noexcept;
987
+
988
+
989
+ /**
990
+ * Compute the number of bytes that this UTF-32 string would require in UTF-8 format.
991
+ *
992
+ * This function does not validate the input. It is acceptable to pass invalid UTF-32 strings but in such cases
993
+ * the result is implementation defined.
994
+ *
995
+ * @param input the UTF-32 string to convert
996
+ * @param length the length of the string in 4-byte code units (char32_t)
997
+ * @return the number of bytes required to encode the UTF-32 string as UTF-8
998
+ */
999
+ simdutf_warn_unused size_t utf8_length_from_utf32(const char32_t * input, size_t length) noexcept;
1000
+
1001
+ /**
1002
+ * Compute the number of two-byte code units that this UTF-32 string would require in UTF-16 format.
1003
+ *
1004
+ * This function does not validate the input. It is acceptable to pass invalid UTF-32 strings but in such cases
1005
+ * the result is implementation defined.
1006
+ *
1007
+ * @param input the UTF-32 string to convert
1008
+ * @param length the length of the string in 4-byte code units (char32_t)
1009
+ * @return the number of bytes required to encode the UTF-32 string as UTF-16
1010
+ */
1011
+ simdutf_warn_unused size_t utf16_length_from_utf32(const char32_t * input, size_t length) noexcept;
1012
+
1013
+ /**
1014
+ * Compute the number of code units that this Latin1 string would require in UTF-16 format.
1015
+ *
1016
+ * @param length the length of the string in Latin1 code units (char)
1017
+ * @return the length of the string in 2-byte code units (char16_t) required to encode the Latin1 string as UTF-16
1018
+ */
1019
+ simdutf_warn_unused size_t utf16_length_from_latin1(size_t length) noexcept;
1020
+
1021
+ /**
1022
+ * Using native endianness; Compute the number of bytes that this UTF-16
1023
+ * string would require in UTF-32 format.
1024
+ *
1025
+ * This function is equivalent to count_utf16.
1026
+ *
1027
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
1028
+ * the result is implementation defined.
1029
+ *
1030
+ * This function is not BOM-aware.
1031
+ *
1032
+ * @param input the UTF-16 string to convert
1033
+ * @param length the length of the string in 2-byte code units (char16_t)
1034
+ * @return the number of bytes required to encode the UTF-16LE string as UTF-32
1035
+ */
1036
+ simdutf_warn_unused size_t utf32_length_from_utf16(const char16_t * input, size_t length) noexcept;
1037
+
1038
+ /**
1039
+ * Compute the number of bytes that this UTF-16LE string would require in UTF-32 format.
1040
+ *
1041
+ * This function is equivalent to count_utf16le.
1042
+ *
1043
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
1044
+ * the result is implementation defined.
1045
+ *
1046
+ * This function is not BOM-aware.
1047
+ *
1048
+ * @param input the UTF-16LE string to convert
1049
+ * @param length the length of the string in 2-byte code units (char16_t)
1050
+ * @return the number of bytes required to encode the UTF-16LE string as UTF-32
1051
+ */
1052
+ simdutf_warn_unused size_t utf32_length_from_utf16le(const char16_t * input, size_t length) noexcept;
1053
+
1054
+ /**
1055
+ * Compute the number of bytes that this UTF-16BE string would require in UTF-32 format.
1056
+ *
1057
+ * This function is equivalent to count_utf16be.
1058
+ *
1059
+ * This function does not validate the input. It is acceptable to pass invalid UTF-16 strings but in such cases
1060
+ * the result is implementation defined.
1061
+ *
1062
+ * This function is not BOM-aware.
1063
+ *
1064
+ * @param input the UTF-16BE string to convert
1065
+ * @param length the length of the string in 2-byte code units (char16_t)
1066
+ * @return the number of bytes required to encode the UTF-16BE string as UTF-32
1067
+ */
1068
+ simdutf_warn_unused size_t utf32_length_from_utf16be(const char16_t * input, size_t length) noexcept;
1069
+
1070
+ /**
1071
+ * Compute the number of bytes that this Latin1 string would require in UTF-32 format.
1072
+ *
1073
+ * @param length the length of the string in Latin1 code units (char)
1074
+ * @return the length of the string in 4-byte code units (char32_t) required to encode the Latin1 string as UTF-32
1075
+ */
1076
+ simdutf_warn_unused size_t utf32_length_from_latin1(size_t length) noexcept;
1077
+ ```
1078
+
1079
+
1080
+
1081
+ We have a wide range of conversion between Latin1, UTF-8, UTF-16 and UTF-32. They assume
1082
+ that you are allocated sufficient memory for the input. The simplest conversion
1083
+ function output a single integer representing the size of the input, with a value of zero
1084
+ indicating an error (e.g., `convert_utf8_to_utf16le`). They are well suited in the
1085
+ scenario where you expect the input to be valid most of the time.
1086
+
1087
+
1088
+
1089
+ ```cpp
1090
+ /**
1091
+ * Convert Latin1 string into UTF-8 string.
1092
+ *
1093
+ * This function is suitable to work with inputs from untrusted sources.
1094
+ *
1095
+ * @param input the Latin1 string to convert
1096
+ * @param length the length of the string in bytes
1097
+ * @param utf8_output the pointer to buffer that can hold conversion result
1098
+ * @return the number of written char; 0 if conversion is not possible
1099
+ */
1100
+ simdutf_warn_unused size_t convert_latin1_to_utf8(const char * input, size_t length, char* utf8_output) noexcept;
1101
+
1102
+ /**
1103
+ * Convert Latin1 string into UTF-8 string with output limit.
1104
+ *
1105
+ * This function is suitable to work with inputs from untrusted sources.
1106
+ *
1107
+ * @param input the Latin1 string to convert
1108
+ * @param length the length of the string in bytes
1109
+ * @param utf8_output the pointer to buffer that can hold conversion result
1110
+ * @param utf8_len the maximum output length
1111
+ * @return the number of written char; 0 if conversion is not possible
1112
+ */
1113
+ simdutf_warn_unused size_t convert_latin1_to_utf8_safe(const char * input, size_t length, char* utf8_output, size_t utf8_len) noexcept;
1114
+
1115
+ /**
1116
+ * Using native endianness, convert a Latin1 string into a UTF-16 string.
1117
+ *
1118
+ * @param input the Latin1 string to convert
1119
+ * @param length the length of the string in bytes
1120
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1121
+ * @return the number of written char16_t.
1122
+ */
1123
+ simdutf_warn_unused size_t convert_latin1_to_utf16(const char * input, size_t length, char16_t* utf16_output) noexcept;
1124
+
1125
+ /**
1126
+ * Convert possibly Latin1 string into UTF-16LE string.
1127
+ *
1128
+ * This function is suitable to work with inputs from untrusted sources.
1129
+ *
1130
+ * @param input the Latin1 string to convert
1131
+ * @param length the length of the string in bytes
1132
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1133
+ * @return the number of written char16_t; 0 if conversion is not possible
1134
+ */
1135
+ simdutf_warn_unused size_t convert_latin1_to_utf16le(const char * input, size_t length, char16_t* utf16_output) noexcept;
1136
+
1137
+ /**
1138
+ * Convert Latin1 string into UTF-16BE string.
1139
+ *
1140
+ * This function is suitable to work with inputs from untrusted sources.
1141
+ *
1142
+ * @param input the Latin1 string to convert
1143
+ * @param length the length of the string in bytes
1144
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1145
+ * @return the number of written char16_t; 0 if conversion is not possible
1146
+ */
1147
+ simdutf_warn_unused size_t convert_latin1_to_utf16be(const char * input, size_t length, char16_t* utf16_output) noexcept;
1148
+
1149
+ /**
1150
+ * Convert Latin1 string into UTF-32 string.
1151
+ *
1152
+ * This function is suitable to work with inputs from untrusted sources.
1153
+ *
1154
+ * @param input the Latin1 string to convert
1155
+ * @param length the length of the string in bytes
1156
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1157
+ * @return the number of written char32_t; 0 if conversion is not possible
1158
+ */
1159
+ simdutf_warn_unused size_t convert_latin1_to_utf32(const char * input, size_t length, char32_t* utf32_buffer) noexcept;
1160
+
1161
+ /**
1162
+ * Convert possibly broken UTF-8 string into Latin1 string.
1163
+ * If the string cannot be represented as Latin1, an error
1164
+ * code is returned.
1165
+ *
1166
+ * During the conversion also validation of the input string is done.
1167
+ * This function is suitable to work with inputs from untrusted sources.
1168
+ *
1169
+ * @param input the UTF-8 string to convert
1170
+ * @param length the length of the string in bytes
1171
+ * @param latin1_output the pointer to buffer that can hold conversion result
1172
+ * @return the number of written char; 0 if the input was not valid UTF-8 string or if it cannot be represented as Latin1
1173
+ */
1174
+ simdutf_warn_unused size_t convert_utf8_to_latin1(const char * input, size_t length, char* latin1_output) noexcept;
1175
+
1176
+ /**
1177
+ * Using native endianness; Convert possibly broken UTF-8 string into a UTF-16 string.
1178
+ *
1179
+ * During the conversion also validation of the input string is done.
1180
+ * This function is suitable to work with inputs from untrusted sources.
1181
+ *
1182
+ * @param input the UTF-8 string to convert
1183
+ * @param length the length of the string in bytes
1184
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1185
+ * @return the number of written char16_t; 0 if the input was not valid UTF-8 string
1186
+ */
1187
+ simdutf_warn_unused size_t convert_utf8_to_utf16(const char * input, size_t length, char16_t* utf16_output) noexcept;
1188
+
1189
+ /**
1190
+ * Convert possibly broken UTF-8 string into UTF-16LE string.
1191
+ *
1192
+ * During the conversion also validation of the input string is done.
1193
+ * This function is suitable to work with inputs from untrusted sources.
1194
+ *
1195
+ * @param input the UTF-8 string to convert
1196
+ * @param length the length of the string in bytes
1197
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1198
+ * @return the number of written char16_t; 0 if the input was not valid UTF-8 string
1199
+ */
1200
+ simdutf_warn_unused size_t convert_utf8_to_utf16le(const char * input, size_t length, char16_t* utf16_output) noexcept;
1201
+
1202
+ /**
1203
+ * Convert possibly broken UTF-8 string into UTF-16BE string.
1204
+ *
1205
+ * During the conversion also validation of the input string is done.
1206
+ * This function is suitable to work with inputs from untrusted sources.
1207
+ *
1208
+ * @param input the UTF-8 string to convert
1209
+ * @param length the length of the string in bytes
1210
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1211
+ * @return the number of written char16_t; 0 if the input was not valid UTF-8 string
1212
+ */
1213
+ simdutf_warn_unused size_t convert_utf8_to_utf16be(const char * input, size_t length, char16_t* utf16_output) noexcept;
1214
+
1215
+ /**
1216
+ * Convert possibly broken UTF-8 string into UTF-32 string.
1217
+ *
1218
+ * During the conversion also validation of the input string is done.
1219
+ * This function is suitable to work with inputs from untrusted sources.
1220
+ *
1221
+ * @param input the UTF-8 string to convert
1222
+ * @param length the length of the string in bytes
1223
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1224
+ * @return the number of written char32_t; 0 if the input was not valid UTF-8 string
1225
+ */
1226
+ simdutf_warn_unused size_t convert_utf8_to_utf32(const char * input, size_t length, char32_t* utf32_output) noexcept;
1227
+
1228
+ /**
1229
+ * Using native endianness, convert possibly broken UTF-16 string into UTF-8
1230
+ * string.
1231
+ *
1232
+ * During the conversion also validation of the input string is done.
1233
+ * This function is suitable to work with inputs from untrusted sources.
1234
+ *
1235
+ * This function is not BOM-aware.
1236
+ *
1237
+ * @param input the UTF-16 string to convert
1238
+ * @param length the length of the string in 2-byte code units (char16_t)
1239
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1240
+ * @return number of written code units; 0 if input is not a valid UTF-16LE
1241
+ * string
1242
+ */
1243
+ simdutf_warn_unused size_t convert_utf16_to_utf8(const char16_t *input,
1244
+ size_t length,
1245
+ char *utf8_buffer) noexcept;
1246
+
1247
+
1248
+ /**
1249
+ * Using native endianness, convert possibly broken UTF-16 string into UTF-8
1250
+ * string with output limit.
1251
+ *
1252
+ * We write as many characters as possible into the output buffer,
1253
+ *
1254
+ * During the conversion also validation of the input string is done.
1255
+ * This function is suitable to work with inputs from untrusted sources.
1256
+ *
1257
+ * This function is not BOM-aware.
1258
+ *
1259
+ *
1260
+ * @param input the UTF-16 string to convert
1261
+ * @param length the length of the string in 16-bit code units (char16_t)
1262
+ * @param utf8_output the pointer to buffer that can hold conversion result
1263
+ * @param utf8_len the maximum output length
1264
+ * @return the number of written char; 0 if conversion is not possible
1265
+ */
1266
+ simdutf_warn_unused size_t
1267
+ convert_utf16_to_utf8_safe(const char16_t *input, size_t length, char *utf8_output,
1268
+ size_t utf8_len) noexcept;
1269
+
1270
+ /**
1271
+ * Using native endianness, convert possibly broken UTF-16 string into Latin1 string.
1272
+ * If the string cannot be represented as Latin1, an error
1273
+ * is returned.
1274
+ *
1275
+ * During the conversion also validation of the input string is done.
1276
+ * This function is suitable to work with inputs from untrusted sources.
1277
+ *
1278
+ * This function is not BOM-aware.
1279
+ *
1280
+ * @param input the UTF-16 string to convert
1281
+ * @param length the length of the string in 2-byte code units (char16_t)
1282
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1283
+ * @return number of written code units; 0 if input is not a valid UTF-16 string or if it cannot be represented as Latin1
1284
+ */
1285
+ simdutf_warn_unused size_t convert_utf16_to_latin1(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1286
+
1287
+ /**
1288
+ * Convert possibly broken UTF-16LE string into Latin1 string.
1289
+ *
1290
+ * During the conversion also validation of the input string is done.
1291
+ * This function is suitable to work with inputs from untrusted sources.
1292
+ *
1293
+ * This function is not BOM-aware.
1294
+ *
1295
+ * @param input the UTF-16LE string to convert
1296
+ * @param length the length of the string in 2-byte code units (char16_t)
1297
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1298
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string or if it cannot be represented as Latin1
1299
+ */
1300
+ simdutf_warn_unused size_t convert_utf16le_to_latin1(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1301
+
1302
+ /**
1303
+ * Convert possibly broken UTF-16BE string into Latin1 string.
1304
+ * If the string cannot be represented as Latin1, an error
1305
+ * is returned.
1306
+ *
1307
+ * During the conversion also validation of the input string is done.
1308
+ * This function is suitable to work with inputs from untrusted sources.
1309
+ *
1310
+ * This function is not BOM-aware.
1311
+ *
1312
+ * @param input the UTF-16BE string to convert
1313
+ * @param length the length of the string in 2-byte code units (char16_t)
1314
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1315
+ * @return number of written code units; 0 if input is not a valid UTF-16BE string or if it cannot be represented as Latin1
1316
+ */
1317
+ simdutf_warn_unused size_t convert_utf16be_to_latin1(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1318
+
1319
+ /**
1320
+ * Convert possibly broken UTF-16LE string into UTF-8 string.
1321
+ *
1322
+ * During the conversion also validation of the input string is done.
1323
+ * This function is suitable to work with inputs from untrusted sources.
1324
+ *
1325
+ * This function is not BOM-aware.
1326
+ *
1327
+ * @param input the UTF-16LE string to convert
1328
+ * @param length the length of the string in 2-byte code units (char16_t)
1329
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1330
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string
1331
+ */
1332
+ simdutf_warn_unused size_t convert_utf16le_to_utf8(const char16_t * input, size_t length, char* utf8_buffer) noexcept;
1333
+
1334
+ /**
1335
+ * Convert possibly broken UTF-16BE string into UTF-8 string.
1336
+ *
1337
+ * During the conversion also validation of the input string is done.
1338
+ * This function is suitable to work with inputs from untrusted sources.
1339
+ *
1340
+ * This function is not BOM-aware.
1341
+ *
1342
+ * @param input the UTF-16BE string to convert
1343
+ * @param length the length of the string in 2-byte code units (char16_t)
1344
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1345
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string
1346
+ */
1347
+ simdutf_warn_unused size_t convert_utf16be_to_utf8(const char16_t * input, size_t length, char* utf8_buffer) noexcept;
1348
+
1349
+
1350
+ /**
1351
+ * Convert possibly broken UTF-32 string into Latin1 string.
1352
+ *
1353
+ * During the conversion also validation of the input string is done.
1354
+ * This function is suitable to work with inputs from untrusted sources.
1355
+ *
1356
+ * This function is not BOM-aware.
1357
+ *
1358
+ * @param input the UTF-32 string to convert
1359
+ * @param length the length of the string in 4-byte code units (char32_t)
1360
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1361
+ * @return number of written code units; 0 if input is not a valid UTF-32 string or if it cannot be represented as Latin1
1362
+ */
1363
+ simdutf_warn_unused size_t convert_utf32_to_latin1(const char32_t * input, size_t length, char* latin1_buffer) noexcept;
1364
+
1365
+ /**
1366
+ * Convert possibly broken UTF-32 string into UTF-8 string.
1367
+ *
1368
+ * During the conversion also validation of the input string is done.
1369
+ * This function is suitable to work with inputs from untrusted sources.
1370
+ *
1371
+ * This function is not BOM-aware.
1372
+ *
1373
+ * @param input the UTF-32 string to convert
1374
+ * @param length the length of the string in 4-byte code units (char32_t)
1375
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1376
+ * @return number of written code units; 0 if input is not a valid UTF-32 string
1377
+ */
1378
+ simdutf_warn_unused size_t convert_utf32_to_utf8(const char32_t * input, size_t length, char* utf8_buffer) noexcept;
1379
+
1380
+ /**
1381
+ * Using native endianness; Convert possibly broken UTF-32 string into a UTF-16 string.
1382
+ *
1383
+ * During the conversion also validation of the input string is done.
1384
+ * This function is suitable to work with inputs from untrusted sources.
1385
+ *
1386
+ * This function is not BOM-aware.
1387
+ *
1388
+ * @param input the UTF-32 string to convert
1389
+ * @param length the length of the string in 4-byte code units (char32_t)
1390
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1391
+ * @return number of written code units; 0 if input is not a valid UTF-32 string
1392
+ */
1393
+ simdutf_warn_unused size_t convert_utf32_to_utf16(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1394
+
1395
+ /**
1396
+ * Convert possibly broken UTF-32 string into UTF-16LE string.
1397
+ *
1398
+ * During the conversion also validation of the input string is done.
1399
+ * This function is suitable to work with inputs from untrusted sources.
1400
+ *
1401
+ * This function is not BOM-aware.
1402
+ *
1403
+ * @param input the UTF-32 string to convert
1404
+ * @param length the length of the string in 4-byte code units (char32_t)
1405
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1406
+ * @return number of written code units; 0 if input is not a valid UTF-32 string
1407
+ */
1408
+ simdutf_warn_unused size_t convert_utf32_to_utf16le(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1409
+
1410
+ /**
1411
+ * Convert possibly broken UTF-32 string into UTF-16BE string.
1412
+ *
1413
+ * During the conversion also validation of the input string is done.
1414
+ * This function is suitable to work with inputs from untrusted sources.
1415
+ *
1416
+ * This function is not BOM-aware.
1417
+ *
1418
+ * @param input the UTF-32 string to convert
1419
+ * @param length the length of the string in 4-byte code units (char32_t)
1420
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1421
+ * @return number of written code units; 0 if input is not a valid UTF-32 string
1422
+ */
1423
+ simdutf_warn_unused size_t convert_utf32_to_utf16be(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1424
+
1425
+ /**
1426
+ * Using native endianness; Convert possibly broken UTF-16 string into UTF-32 string.
1427
+ *
1428
+ * During the conversion also validation of the input string is done.
1429
+ * This function is suitable to work with inputs from untrusted sources.
1430
+ *
1431
+ * This function is not BOM-aware.
1432
+ *
1433
+ * @param input the UTF-16 string to convert
1434
+ * @param length the length of the string in 2-byte code units (char16_t)
1435
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1436
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string
1437
+ */
1438
+ simdutf_warn_unused size_t convert_utf16_to_utf32(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1439
+
1440
+ /**
1441
+ * Convert possibly broken UTF-16LE string into UTF-32 string.
1442
+ *
1443
+ * During the conversion also validation of the input string is done.
1444
+ * This function is suitable to work with inputs from untrusted sources.
1445
+ *
1446
+ * This function is not BOM-aware.
1447
+ *
1448
+ * @param input the UTF-16LE string to convert
1449
+ * @param length the length of the string in 2-byte code units (char16_t)
1450
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1451
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string
1452
+ */
1453
+ simdutf_warn_unused size_t convert_utf16le_to_utf32(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1454
+
1455
+ /**
1456
+ * Convert possibly broken UTF-16BE string into UTF-32 string.
1457
+ *
1458
+ * During the conversion also validation of the input string is done.
1459
+ * This function is suitable to work with inputs from untrusted sources.
1460
+ *
1461
+ * This function is not BOM-aware.
1462
+ *
1463
+ * @param input the UTF-16BE string to convert
1464
+ * @param length the length of the string in 2-byte code units (char16_t)
1465
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1466
+ * @return number of written code units; 0 if input is not a valid UTF-16LE string
1467
+ */
1468
+ simdutf_warn_unused size_t convert_utf16be_to_utf32(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1469
+ ```
1470
+
1471
+ In some cases, you need to transcode UTF-8 or UTF-16 inputs, but you may have a truncated
1472
+ string, meaning that the last character might be incomplete. In such cases, we recommend
1473
+ trimming the end of your input so you do not encounter an error.
1474
+
1475
+ ```cpp
1476
+
1477
+ /**
1478
+ * Given a valid UTF-8 string having a possibly truncated last character,
1479
+ * this function checks the end of string. If the last character is truncated (or partial),
1480
+ * then it returns a shorter length (shorter by 1 to 3 bytes) so that the short UTF-8
1481
+ * strings only contain complete characters. If there is no truncated character,
1482
+ * the original length is returned.
1483
+ *
1484
+ * This function assumes that the input string is valid UTF-8, but possibly truncated.
1485
+ *
1486
+ * @param input the UTF-8 string to process
1487
+ * @param length the length of the string in bytes
1488
+ * @return the length of the string in bytes, possibly shorter by 1 to 3 bytes
1489
+ */
1490
+ simdutf_warn_unused size_t trim_partial_utf8(const char *input, size_t length);
1491
+
1492
+ /**
1493
+ * Given a valid UTF-16BE string having a possibly truncated last character,
1494
+ * this function checks the end of string. If the last character is truncated (or partial),
1495
+ * then it returns a shorter length (shorter by 1 unit) so that the short UTF-16BE
1496
+ * strings only contain complete characters. If there is no truncated character,
1497
+ * the original length is returned.
1498
+ *
1499
+ * This function assumes that the input string is valid UTF-16BE, but possibly truncated.
1500
+ *
1501
+ * @param input the UTF-16BE string to process
1502
+ * @param length the length of the string in bytes
1503
+ * @return the length of the string in bytes, possibly shorter by 1 unit
1504
+ */
1505
+ simdutf_warn_unused size_t trim_partial_utf16be(const char16_t* input, size_t length);
1506
+
1507
+ /**
1508
+ * Given a valid UTF-16LE string having a possibly truncated last character,
1509
+ * this function checks the end of string. If the last character is truncated (or partial),
1510
+ * then it returns a shorter length (shorter by 1 unit) so that the short UTF-16LE
1511
+ * strings only contain complete characters. If there is no truncated character,
1512
+ * the original length is returned.
1513
+ *
1514
+ * This function assumes that the input string is valid UTF-16LE, but possibly truncated.
1515
+ *
1516
+ * @param input the UTF-16LE string to process
1517
+ * @param length the length of the string in bytes
1518
+ * @return the length of the string in unit, possibly shorter by 1 unit
1519
+ */
1520
+ simdutf_warn_unused size_t trim_partial_utf16le(const char16_t* input, size_t length);
1521
+
1522
+
1523
+ /**
1524
+ * Given a valid UTF-16 string having a possibly truncated last character,
1525
+ * this function checks the end of string. If the last character is truncated (or partial),
1526
+ * then it returns a shorter length (shorter by 1 unit) so that the short UTF-16
1527
+ * strings only contain complete characters. If there is no truncated character,
1528
+ * the original length is returned.
1529
+ *
1530
+ * This function assumes that the input string is valid UTF-16, but possibly truncated.
1531
+ * We use the native endianness.
1532
+ *
1533
+ * @param input the UTF-16 string to process
1534
+ * @param length the length of the string in bytes
1535
+ * @return the length of the string in unit, possibly shorter by 1 unit
1536
+ */
1537
+ simdutf_warn_unused size_t trim_partial_utf16(const char16_t* input, size_t length);
1538
+ ```
1539
+
1540
+ You may use these `trim_` functions to decode inputs piece by piece, as in the following
1541
+ examples. First a case where you want to decode a UTF-8 strings in two steps:
1542
+
1543
+ ```cpp
1544
+ const char unicode[] = "\xc3\xa9\x63ole d'\xc3\xa9t\xc3\xa9";
1545
+ // suppose you want to decode only the start of this string.
1546
+ size_t length = 10;
1547
+ // Picking 10 bytes is problematic because we might end up in the middle of a
1548
+ // code point. But we can rewind to the previous code point.
1549
+ length = simdutf::trim_partial_utf8(unicode, length);
1550
+ // Now we can transcode safely
1551
+ size_t budget_utf16 = simdutf::utf16_length_from_utf8(unicode, length);
1552
+ std::unique_ptr<char16_t[]> utf16{new char16_t[budget_utf16]};
1553
+ size_t utf16words =
1554
+ simdutf::convert_utf8_to_utf16le(unicode, length, utf16.get());
1555
+ // We can then transcode the next batch
1556
+ const char * next = unicode + length;
1557
+ size_t next_length = sizeof(unicode) - length;
1558
+ size_t next_budget_utf16 = simdutf::utf16_length_from_utf8(next, next_length);
1559
+ std::unique_ptr<char16_t[]> next_utf16{new char16_t[next_budget_utf16]};
1560
+ size_t next_utf16words =
1561
+ simdutf::convert_utf8_to_utf16le(next, next_length, next_utf16.get());
1562
+ ```
1563
+
1564
+ You can use the same approach with UTF-16:
1565
+
1566
+ ```cpp // We have three sequences of surrogate pairs (UTF-16).
1567
+ const char16_t unicode[] = u"\x3cd8\x10df\x3cd8\x10df\x3cd8\x10df";
1568
+ // suppose you want to decode only the start of this string.
1569
+ size_t length = 3;
1570
+ // Picking 3 units is problematic because we might end up in the middle of a
1571
+ // surrogate pair. But we can rewind to the previous code point.
1572
+ length = simdutf::trim_partial_utf16(unicode, length);
1573
+ // Now we can transcode safely
1574
+ size_t budget_utf8 = simdutf::utf8_length_from_utf16(unicode, length);
1575
+ std::unique_ptr<char[]> utf8{new char[budget_utf8]};
1576
+ size_t utf8words =
1577
+ simdutf::convert_utf16_to_utf8(unicode, length, utf8.get());
1578
+ // We can then transcode the next batch
1579
+ const char16_t * next = unicode + length;
1580
+ size_t next_length = 6 - length;
1581
+ size_t next_budget_utf8 = simdutf::utf8_length_from_utf16(next, next_length);
1582
+ std::unique_ptr<char[]> next_utf8{new char[next_budget_utf8]};
1583
+ size_t next_utf8words =
1584
+ simdutf::convert_utf16_to_utf8(next, next_length, next_utf8.get());
1585
+ ```
1586
+
1587
+
1588
+ We have more advanced conversion functions which output a `simdutf::result` structure with
1589
+ an indication of the error type and a `count` entry (e.g., `convert_utf8_to_utf16le_with_errors`).
1590
+ They are well suited when you expect that there might be errors in the input that require
1591
+ further investigation. The `count` field contains the location of the error in the input in code units,
1592
+ if there is an error, or otherwise the number of code units written. You may use these functions as follows:
1593
+
1594
+ ```cpp
1595
+ // this UTF-8 string has a bad byte at index 5
1596
+ std::string bad_utf8 = "\xc3\xa9\xc3\xa9\x20\xff\xc3\xa9";
1597
+ size_t budget_utf16 = simdutf::utf16_length_from_utf8(bad_utf8.data(), bad_utf8.size());
1598
+ std::unique_ptr<char16_t[]> utf16{new char16_t[budget_utf16]};
1599
+ simdutf::result res = simdutf::convert_utf8_to_utf16_with_errors(bad_utf8.data(), bad_utf8.size(), utf16.get());
1600
+ if(res.error != simdutf::error_code::SUCCESS) {
1601
+ std::cerr << "error at index " << res.count << std::endl;
1602
+ }
1603
+ // the following will be successful
1604
+ res = simdutf::convert_utf8_to_utf16_with_errors(bad_utf8.data(), res.count, utf16.get());
1605
+ if(res.error == simdutf::error_code::SUCCESS) {
1606
+ std::cerr << "we have transcoded " << res.count << " characters" << std::endl;
1607
+ }
1608
+ ```
1609
+
1610
+ We have several transcoding functions returning `simdutf::error` results:
1611
+
1612
+ ```cpp
1613
+ /**
1614
+ * Convert possibly broken UTF-8 string into Latin1 string with errors.
1615
+ * If the string cannot be represented as Latin1, an error
1616
+ * code is returned.
1617
+ *
1618
+ * During the conversion also validation of the input string is done.
1619
+ * This function is suitable to work with inputs from untrusted sources.
1620
+ *
1621
+ * @param input the UTF-8 string to convert
1622
+ * @param length the length of the string in bytes
1623
+ * @param latin1_output the pointer to buffer that can hold conversion result
1624
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of code units validated if successful.
1625
+ */
1626
+ simdutf_warn_unused result convert_utf8_to_latin1_with_errors(const char * input, size_t length, char* latin1_output) noexcept;
1627
+
1628
+ /**
1629
+ * Convert possibly broken UTF-16LE string into Latin1 string.
1630
+ * If the string cannot be represented as Latin1, an error
1631
+ * is returned.
1632
+ *
1633
+ * During the conversion also validation of the input string is done.
1634
+ * This function is suitable to work with inputs from untrusted sources.
1635
+ * This function is not BOM-aware.
1636
+ *
1637
+ * @param input the UTF-16LE string to convert
1638
+ * @param length the length of the string in 2-byte code units (char16_t)
1639
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1640
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1641
+ */
1642
+ simdutf_warn_unused result convert_utf16le_to_latin1_with_errors(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1643
+
1644
+ /**
1645
+ * Convert possibly broken UTF-16BE string into Latin1 string.
1646
+ * If the string cannot be represented as Latin1, an error
1647
+ * is returned.
1648
+ *
1649
+ * During the conversion also validation of the input string is done.
1650
+ * This function is suitable to work with inputs from untrusted sources.
1651
+ * This function is not BOM-aware.
1652
+ *
1653
+ * @param input the UTF-16BE string to convert
1654
+ * @param length the length of the string in 2-byte code units (char16_t)
1655
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1656
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1657
+ */
1658
+ simdutf_warn_unused result convert_utf16be_to_latin1_with_errors(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1659
+
1660
+ /**
1661
+ * Using native endianness, convert possibly broken UTF-16 string into Latin1 string.
1662
+ * If the string cannot be represented as Latin1, an error
1663
+ * is returned.
1664
+ *
1665
+ * During the conversion also validation of the input string is done.
1666
+ * This function is suitable to work with inputs from untrusted sources.
1667
+ * This function is not BOM-aware.
1668
+ *
1669
+ * @param input the UTF-16 string to convert
1670
+ * @param length the length of the string in 2-byte code units (char16_t)
1671
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1672
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1673
+ */
1674
+ simdutf_warn_unused result convert_utf16_to_latin1_with_errors(const char16_t * input, size_t length, char* latin1_buffer) noexcept;
1675
+
1676
+
1677
+ /**
1678
+ * Using native endianness; Convert possibly broken UTF-8 string into UTF-16
1679
+ * string and stop on error.
1680
+ *
1681
+ * During the conversion also validation of the input string is done.
1682
+ * This function is suitable to work with inputs from untrusted sources.
1683
+ *
1684
+ * @param input the UTF-8 string to convert
1685
+ * @param length the length of the string in bytes
1686
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1687
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1688
+ */
1689
+ simdutf_warn_unused result convert_utf8_to_utf16_with_errors(const char * input, size_t length, char16_t* utf16_output) noexcept;
1690
+
1691
+ /**
1692
+ * Convert possibly broken UTF-8 string into UTF-16LE string and stop on error.
1693
+ *
1694
+ * During the conversion also validation of the input string is done.
1695
+ * This function is suitable to work with inputs from untrusted sources.
1696
+ *
1697
+ * @param input the UTF-8 string to convert
1698
+ * @param length the length of the string in bytes
1699
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1700
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1701
+ */
1702
+ simdutf_warn_unused result convert_utf8_to_utf16le_with_errors(const char * input, size_t length, char16_t* utf16_output) noexcept;
1703
+
1704
+ /**
1705
+ * Convert possibly broken UTF-8 string into UTF-16BE string and stop on error.
1706
+ *
1707
+ * During the conversion also validation of the input string is done.
1708
+ * This function is suitable to work with inputs from untrusted sources.
1709
+ *
1710
+ * @param input the UTF-8 string to convert
1711
+ * @param length the length of the string in bytes
1712
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1713
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1714
+ */
1715
+ simdutf_warn_unused result convert_utf8_to_utf16be_with_errors(const char * input, size_t length, char16_t* utf16_output) noexcept;
1716
+
1717
+ /**
1718
+ * Convert possibly broken UTF-8 string into UTF-32 string and stop on error.
1719
+ *
1720
+ * During the conversion also validation of the input string is done.
1721
+ * This function is suitable to work with inputs from untrusted sources.
1722
+ *
1723
+ * @param input the UTF-8 string to convert
1724
+ * @param length the length of the string in bytes
1725
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1726
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char32_t written if successful.
1727
+ */
1728
+ simdutf_warn_unused result convert_utf8_to_utf32_with_errors(const char * input, size_t length, char32_t* utf32_output) noexcept;
1729
+
1730
+
1731
+ /**
1732
+ * Convert possibly broken UTF-16LE string into UTF-8 string and stop on error.
1733
+ *
1734
+ * During the conversion also validation of the input string is done.
1735
+ * This function is suitable to work with inputs from untrusted sources.
1736
+ *
1737
+ * This function is not BOM-aware.
1738
+ *
1739
+ * @param input the UTF-16LE string to convert
1740
+ * @param length the length of the string in 2-byte code units (char16_t)
1741
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1742
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1743
+ */
1744
+ simdutf_warn_unused result convert_utf16le_to_utf8_with_errors(const char16_t * input, size_t length, char* utf8_buffer) noexcept;
1745
+
1746
+ /**
1747
+ * Convert possibly broken UTF-16BE string into UTF-8 string and stop on error.
1748
+ *
1749
+ * During the conversion also validation of the input string is done.
1750
+ * This function is suitable to work with inputs from untrusted sources.
1751
+ *
1752
+ * This function is not BOM-aware.
1753
+ *
1754
+ * @param input the UTF-16BE string to convert
1755
+ * @param length the length of the string in 2-byte code units (char16_t)
1756
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1757
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1758
+ */
1759
+ simdutf_warn_unused result convert_utf16be_to_utf8_with_errors(const char16_t * input, size_t length, char* utf8_buffer) noexcept;
1760
+
1761
+
1762
+ /**
1763
+ * Convert possibly broken UTF-32 string into Latin1 string and stop on error.
1764
+ * If the string cannot be represented as Latin1, an error is returned.
1765
+ *
1766
+ * During the conversion also validation of the input string is done.
1767
+ * This function is suitable to work with inputs from untrusted sources.
1768
+ *
1769
+ * This function is not BOM-aware.
1770
+ *
1771
+ * @param input the UTF-32 string to convert
1772
+ * @param length the length of the string in 4-byte code units (char32_t)
1773
+ * @param latin1_buffer the pointer to buffer that can hold conversion result
1774
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1775
+ */
1776
+ simdutf_warn_unused result convert_utf32_to_latin1_with_errors(const char32_t * input, size_t length, char* latin1_buffer) noexcept;
1777
+
1778
+
1779
+ /**
1780
+ * Convert possibly broken UTF-32 string into UTF-8 string and stop on error.
1781
+ *
1782
+ * During the conversion also validation of the input string is done.
1783
+ * This function is suitable to work with inputs from untrusted sources.
1784
+ *
1785
+ * This function is not BOM-aware.
1786
+ *
1787
+ * @param input the UTF-32 string to convert
1788
+ * @param length the length of the string in 4-byte code units (char32_t)
1789
+ * @param utf8_buffer the pointer to buffer that can hold conversion result
1790
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char written if successful.
1791
+ */
1792
+ simdutf_warn_unused result convert_utf32_to_utf8_with_errors(const char32_t * input, size_t length, char* utf8_buffer) noexcept;
1793
+
1794
+ /**
1795
+ * Using native endianness; Convert possibly broken UTF-32 string into UTF-16
1796
+ * string and stop on error.
1797
+ *
1798
+ * During the conversion also validation of the input string is done.
1799
+ * This function is suitable to work with inputs from untrusted sources.
1800
+ *
1801
+ * This function is not BOM-aware.
1802
+ *
1803
+ * @param input the UTF-32 string to convert
1804
+ * @param length the length of the string in 4-byte code units (char32_t)
1805
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1806
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1807
+ */
1808
+ simdutf_warn_unused result convert_utf32_to_utf16_with_errors(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1809
+
1810
+ /**
1811
+ * Convert possibly broken UTF-32 string into UTF-16LE string and stop on error.
1812
+ *
1813
+ * During the conversion also validation of the input string is done.
1814
+ * This function is suitable to work with inputs from untrusted sources.
1815
+ *
1816
+ * This function is not BOM-aware.
1817
+ *
1818
+ * @param input the UTF-32 string to convert
1819
+ * @param length the length of the string in 4-byte code units (char32_t)
1820
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1821
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1822
+ */
1823
+ simdutf_warn_unused result convert_utf32_to_utf16le_with_errors(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1824
+
1825
+ /**
1826
+ * Convert possibly broken UTF-32 string into UTF-16BE string and stop on error.
1827
+ *
1828
+ * During the conversion also validation of the input string is done.
1829
+ * This function is suitable to work with inputs from untrusted sources.
1830
+ *
1831
+ * This function is not BOM-aware.
1832
+ *
1833
+ * @param input the UTF-32 string to convert
1834
+ * @param length the length of the string in 4-byte code units (char32_t)
1835
+ * @param utf16_buffer the pointer to buffer that can hold conversion result
1836
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char16_t written if successful.
1837
+ */
1838
+ simdutf_warn_unused result convert_utf32_to_utf16be_with_errors(const char32_t * input, size_t length, char16_t* utf16_buffer) noexcept;
1839
+
1840
+ /**
1841
+ * Using native endianness; Convert possibly broken UTF-16 string into
1842
+ * UTF-32 string and stop on error.
1843
+ *
1844
+ * During the conversion also validation of the input string is done.
1845
+ * This function is suitable to work with inputs from untrusted sources.
1846
+ *
1847
+ * This function is not BOM-aware.
1848
+ *
1849
+ * @param input the UTF-16 string to convert
1850
+ * @param length the length of the string in 2-byte code units (char16_t)
1851
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1852
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char32_t written if successful.
1853
+ */
1854
+ simdutf_warn_unused result convert_utf16_to_utf32_with_errors(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1855
+
1856
+ /**
1857
+ * Convert possibly broken UTF-16LE string into UTF-32 string and stop on error.
1858
+ *
1859
+ * During the conversion also validation of the input string is done.
1860
+ * This function is suitable to work with inputs from untrusted sources.
1861
+ *
1862
+ * This function is not BOM-aware.
1863
+ *
1864
+ * @param input the UTF-16LE string to convert
1865
+ * @param length the length of the string in 2-byte code units (char16_t)
1866
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1867
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char32_t written if successful.
1868
+ */
1869
+ simdutf_warn_unused result convert_utf16le_to_utf32_with_errors(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1870
+
1871
+ /**
1872
+ * Convert possibly broken UTF-16BE string into UTF-32 string and stop on error.
1873
+ *
1874
+ * During the conversion also validation of the input string is done.
1875
+ * This function is suitable to work with inputs from untrusted sources.
1876
+ *
1877
+ * This function is not BOM-aware.
1878
+ *
1879
+ * @param input the UTF-16BE string to convert
1880
+ * @param length the length of the string in 2-byte code units (char16_t)
1881
+ * @param utf32_buffer the pointer to buffer that can hold conversion result
1882
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the error (in the input in code units) if any, or the number of char32_t written if successful.
1883
+ */
1884
+ simdutf_warn_unused result convert_utf16be_to_utf32_with_errors(const char16_t * input, size_t length, char32_t* utf32_buffer) noexcept;
1885
+
1886
+ ```
1887
+
1888
+ If you have a UTF-16 input, you may change its endianness with a fast function.
1889
+
1890
+ ```cpp
1891
+ /**
1892
+ * Change the endianness of the input. Can be used to go from UTF-16LE to UTF-16BE or
1893
+ * from UTF-16BE to UTF-16LE.
1894
+ *
1895
+ * This function does not validate the input.
1896
+ *
1897
+ * This function is not BOM-aware.
1898
+ *
1899
+ * @param input the UTF-16 string to process
1900
+ * @param length the length of the string in 2-byte code units (char16_t)
1901
+ * @param output the pointer to a buffer that can hold the conversion result
1902
+ */
1903
+ void change_endianness_utf16(const char16_t * input, size_t length, char16_t * output) noexcept;
1904
+
1905
+ ```
1906
+
1907
+ ## Base64
1908
+
1909
+ The WHATWG (Web Hypertext Application Technology Working Group) defines a "forgiving" base64 decoding algorithm in its Infra Standard, which is used in web contexts like the JavaScript atob() function. This algorithm is more lenient than strict RFC 4648 base64, primarily to handle common web data variations. It ignores all ASCII whitespace (spaces, tabs, newlines, etc.), allows omitting padding characters (=), and decodes inputs as long as they meet certain length and character validity rules. However, it still rejects inputs that could lead to ambiguous or incomplete byte formation.
1910
+
1911
+ We also converting from [WHATWG forgiving-base64](https://infra.spec.whatwg.org/#forgiving-base64-decode) to binary, and back. In particular, you can convert base64 inputs which contain ASCII spaces (' ', '\t', '\n', '\r', '\f') to binary. We also support the base64 URL encoding alternative. These functions are part of the Node.js JavaScript runtime: in particular `atob` in Node.js relies on simdutf.
1912
+
1913
+
1914
+ The key steps in this algorithm are:
1915
+ - Remove all whitespace from the input string.
1916
+ - If the resulting string's length is a multiple of 4 and it ends with one or two '=' characters, remove those '=' from the end (treating them as optional padding).
1917
+ - If the length (after any padding removal) modulo 4 equals 1, the input is invalid— this prevents cases where the bit count wouldn't align properly to form whole bytes.
1918
+ - Check that all remaining characters are valid base64 symbols (A-Z, a-z, 0-9, +, /, or =); otherwise, invalid.
1919
+ - Decode by converting each character to its 6-bit value, concatenating the bits, and grouping them into 8-bit bytes. At the end, if there are leftover bits (12 or 18), form as many full bytes as possible and discard the trailing bits (4 or 2, respectively), assuming they are padding zeros.
1920
+
1921
+ This forgiving approach makes base64 decoding robust for web use, but it enforces rules to avoid data corruption.
1922
+
1923
+ The conversion of binary data to base64 always succeeds and is relatively simple. Suppose
1924
+ that you have an original input of binary data `source` (e.g., `std::vector<char>`).
1925
+ ```cpp
1926
+ std::vector<char> buffer(simdutf::base64_length_from_binary(source.size()));
1927
+ simdutf::binary_to_base64(source.data(), source.size(), buffer.data());
1928
+ ```
1929
+
1930
+ Decoding base64 requires validation and, thus, error handling. Furthermore, because
1931
+ we prune ASCII spaces, we may need to adjust the result size afterward.
1932
+
1933
+ ```cpp
1934
+ std::vector<char> buffer(simdutf::maximal_binary_length_from_base64(base64.data(), base64.size()));
1935
+ simdutf::result r = simdutf::base64_to_binary(base64.data(), base64.size(), buffer.data());
1936
+ if(r.error) {
1937
+ // We have some error, r.count tells you where the error was encountered in the input if
1938
+ // the error is INVALID_BASE64_CHARACTER. If the error is BASE64_INPUT_REMAINDER, then
1939
+ // a single valid base64 character remained, and r.count contains the number of bytes decoded.
1940
+ } else {
1941
+ buffer.resize(r.count); // resize the buffer according to actual number of bytes
1942
+ }
1943
+ ```
1944
+
1945
+ You can calculate the exact output space needed by using
1946
+ `binary_length_from_base64` which produces an exact number of output
1947
+ bytes if the input is well-formed. Well-formed means it contains
1948
+ only valid base64 and ASCII whitespace. Invalid input can be given to
1949
+ `binary_length_from_base64`. It will not detect invalid input, but the
1950
+ result can be safely used to size the output buffer for `base64_to_binary`,
1951
+ which does detect invalid input.
1952
+
1953
+ ```cpp
1954
+ std::vector<char> buffer(simdutf::binary_length_from_base64(base64.data(), base64.size()));
1955
+ simdutf::result r = simdutf::base64_to_binary(base64.data(), base64.size(), buffer.data());
1956
+ if (r.error != simdutf::SUCCESS) {
1957
+ // handle error
1958
+ } else {
1959
+ // buffer is already the exact size, no resize needed
1960
+ assert(buffer.size() == r.count);
1961
+ }
1962
+ ```
1963
+
1964
+ Let us consider concrete examples. Take the following strings:
1965
+ `" A A "`, `" A A G A / v 8 "`, `" A A G A / v 8 = "`, `" A A G A / v 8 = = "`.
1966
+ They are all valid WHATWG base64 inputs, except for the last one.
1967
+
1968
+ - The first string, `" A A "`, becomes "AA" after whitespace removal. Its length is 2, and 2 % 4 = 2 (not 1), so it's valid. Decoding: 'A' is 000000 and 'A' is 000000, giving 12 bits (000000000000). Form one byte from the first 8 bits (00000000 = 0x00) and discard the last 4 bits (0000). Result: a single byte value of 0.
1969
+ - The second string, `" A A G A / v 8 "`, becomes "AAGA/v8" (length 7, 7 % 4 = 3, not 1—valid). Decoding the 42 bits yields the byte sequence 0x00, 0x01, 0x80, 0xFE, 0xFF (as you noted; the process groups full 24-bit chunks into three bytes each, then handles the remaining 18 bits as two bytes, discarding the last 2 bits).
1970
+ - The third string, `" A A G A / v 8 = "`, becomes "AAGA/v8=" (length 8, 8 % 4 = 0). It ends with one '=', so remove it, leaving "AAGA/v8" (same as the second example). Valid, and decodes to the same byte sequence: 0x00, 0x01, 0x80, 0xFE, 0xFF.
1971
+ - The fourth string, `" A A G A / v 8 = = "`, becomes "AAGA/v8==" (length 9, 9 % 4 = 1). The length isn't a multiple of 4, so the algorithm doesn't remove the trailing '=='. Since the length modulo 4 is 1, it's invalid. This rule exists because a remainder of 1 would leave only 6 leftover bits after full bytes, which can't form a complete byte (unlike remainders of 2 or 3, which leave 12 or 18 bits and allow discarding 4 or 2 bits). Adding extra '=' here disrupts the expected alignment without qualifying for padding removal.
1972
+
1973
+ Let us process them with actual code.
1974
+
1975
+ ```cpp
1976
+ std::vector<std::string> sources = {
1977
+ " A A ", " A A G A / v 8 ", " A A G A / v 8 = ", " A A G A / v 8 = = "};
1978
+ std::vector<std::vector<uint8_t>> expected = {
1979
+ {0}, {0, 0x1, 0x80, 0xfe, 0xff}, {0, 0x1, 0x80, 0xfe, 0xff}, {}}; // last one is in error
1980
+ for(size_t i = 0; i < sources.size(); i++) {
1981
+ const std::string &source = sources[i];
1982
+ std::cout << "source: '" << source << "'" << std::endl;
1983
+ // allocate enough memory for the maximal binary length
1984
+ std::vector<uint8_t> buffer(simdutf::maximal_binary_length_from_base64(
1985
+ source.data(), source.size()));
1986
+ // convert to binary and check for errors
1987
+ simdutf::result r = simdutf::base64_to_binary(
1988
+ source.data(), source.size(), (char*)buffer.data());
1989
+ if(r.error != simdutf::error_code::SUCCESS) {
1990
+ // We have that expected[i].empty().
1991
+ std::cout << "output: error" << std::endl;
1992
+ } else {
1993
+ buffer.resize(r.count); // in case of success, r.count contains the output length
1994
+ // We have that buffer == expected[i]
1995
+ std::cout << "output: " << r.count << " bytes" << std::endl;
1996
+ }
1997
+ }
1998
+ ```
1999
+
2000
+ This code should print the following:
2001
+
2002
+ ```
2003
+ source: ' A A '
2004
+ output: 1 bytes
2005
+ source: ' A A G A / v 8 '
2006
+ output: 5 bytes
2007
+ source: ' A A G A / v 8 = '
2008
+ output: 5 bytes
2009
+ source: ' A A G A / v 8 = = '
2010
+ output: error
2011
+ ```
2012
+
2013
+ As you can see, the result is as expected.
2014
+
2015
+ In some instances, you may want to limit the size of the output further when decoding base64.
2016
+ For this purpose, you may use the `base64_to_binary_safe` functions. The functions may also
2017
+ be useful if you seek to decode the input into segments having a maximal capacity.
2018
+ Another benefit of the `base64_to_binary_safe` functions is that they inform you
2019
+ about how much data was written to the output buffer, even when there is a fatal
2020
+ error.
2021
+ This number might not be 'maximal': our fast functions may leave some data that could
2022
+ have been decoded prior to a bad character undecoded. With the
2023
+ `base64_to_binary_safe` function, you also have the option of requesting that as much
2024
+ of the data as possible is decoded despite the error by setting the `decode_up_to_bad_char`
2025
+ parameter to true (it defaults to false for best performance).
2026
+
2027
+
2028
+ ```cpp
2029
+ size_t len = 72; // for simplicity we chose len divisible by 3
2030
+ std::vector<char> base64(len, 'a'); // we want to decode 'aaaaa....'
2031
+ std::vector<char> back((len + 3) / 4 * 3);
2032
+ size_t limited_length = back.size() / 2; // Intentionally too small
2033
+ // We proceed to decode half:
2034
+ simdutf::result r = simdutf::base64_to_binary_safe(
2035
+ base64.data(), base64.size(), back.data(), limited_length);
2036
+ assert(r.error == simdutf::error_code::OUTPUT_BUFFER_TOO_SMALL);
2037
+ // We decoded r.count base64 8-bit units to limited_length bytes
2038
+ // Now let us decode the rest !!!
2039
+ //
2040
+ // We have read up to r.count in the input buffer and we have
2041
+ // produced limited_length bytes.
2042
+ //
2043
+ size_t input_index = r.count;
2044
+ size_t limited_length2 = back.size();
2045
+ r = simdutf::base64_to_binary_safe(base64.data() + input_index,
2046
+ base64.size() - input_index,
2047
+ back.data(), limited_length2);
2048
+ assert(r.error == simdutf::error_code::SUCCESS);
2049
+ // We decoded r.count base64 8-bit units to limited_length2 bytes
2050
+ // We are done
2051
+ assert(limited_length2 + limited_length == (len + 3) / 4 * 3);
2052
+ ```
2053
+
2054
+ We can repeat our previous examples with the various spaced strings using
2055
+ `base64_to_binary_safe`. It works much the same except that the convention
2056
+ for the content of `result.count` differs. The output size is stored
2057
+ by reference in the output length parameter.
2058
+
2059
+ ```cpp
2060
+
2061
+ std::vector<std::string> sources = {
2062
+ " A A ", " A A G A / v 8 ", " A A G A / v 8 = ", " A A G A / v 8 = = "};
2063
+ std::vector<std::vector<uint8_t>> expected = {
2064
+ {0}, {0, 0x1, 0x80, 0xfe, 0xff}, {0, 0x1, 0x80, 0xfe, 0xff}, {}}; // last one is in error
2065
+ for(size_t i = 0; i < sources.size(); i++) {
2066
+ const std::string &source = sources[i];
2067
+ std::cout << "source: '" << source << "'" << std::endl;
2068
+ // allocate enough memory for the maximal binary length
2069
+ std::vector<uint8_t> buffer(simdutf::maximal_binary_length_from_base64(
2070
+ source.data(), source.size()));
2071
+ // convert to binary and check for errors
2072
+ size_t output_length = buffer.size();
2073
+ simdutf::result r = simdutf::base64_to_binary_safe(
2074
+ source.data(), source.size(), (char*)buffer.data(), output_length);
2075
+ if(r.error != simdutf::error_code::SUCCESS) {
2076
+ // We have expected[i].empty()
2077
+ std::cout << "output: error" << std::endl;
2078
+ } else {
2079
+ buffer.resize(output_length); // in case of success, output_length contains the output length
2080
+ // We have buffer == expected[i])
2081
+ std::cout << "output: " << output_length << " bytes" << std::endl;
2082
+ std::cout << "input (consumed): " << r.count << " bytes" << std::endl;
2083
+ }
2084
+ ```
2085
+
2086
+ This code should output the following:
2087
+
2088
+ ```
2089
+ source: ' A A '
2090
+ output: 1 bytes
2091
+ input (consumed): 8 bytes
2092
+ source: ' A A G A / v 8 '
2093
+ output: 5 bytes
2094
+ input (consumed): 23 bytes
2095
+ source: ' A A G A / v 8 = '
2096
+ output: 5 bytes
2097
+ input (consumed): 26 bytes
2098
+ source: ' A A G A / v 8 = = '
2099
+ output: error
2100
+ ```
2101
+
2102
+ See our function specifications for more details.
2103
+
2104
+ In other instances, you may receive your base64 inputs in 16-bit units (e.g., from UTF-16 strings):
2105
+ we have function overloads for these cases as well.
2106
+
2107
+ Some users may want to decode the base64 inputs in chunks, especially when doing
2108
+ file or networking programming. These users should see `tools/fastbase64.cpp`, a command-line
2109
+ utility designed for as an example. It reads and writes base64 files using chunks of at most
2110
+ a few tens of kilobytes.
2111
+
2112
+
2113
+ We support two conventions: `base64_default` and `base64_url`:
2114
+ * The default (`base64_default`) includes the characters `+` and `/` as part of its alphabet. It also
2115
+ pads the output with the padding character (`=`) so that the output is divisible by 4. Thus, we have
2116
+ that the string `"Hello, World!"` is encoded to `"SGVsbG8sIFdvcmxkIQ=="` with an expression such as
2117
+ `simdutf::binary_to_base64(source, size, out, simdutf::base64_default)`.
2118
+ When using the default, you can omit the option parameter for simplicity:
2119
+ `simdutf::binary_to_base64(source, size, out, buffer.data())`. When decoding, white space
2120
+ characters are omitted as per the [WHATWG forgiving-base64](https://infra.spec.whatwg.org/#forgiving-base64-decode) standard. Further, if padding characters are present at the end of the
2121
+ stream, there must be no more than two, and if there are any, the total number of characters (excluding
2122
+ ASCII spaces ' ', '\t', '\n', '\r', '\f' but including padding characters) must be divisible by four.
2123
+ * The URL convention (`base64_url`) uses the characters `-` and `_` as part of its alphabet. It does
2124
+ not pad its output. Thus, we have that the string `"Hello, World!"` is encoded to `"SGVsbG8sIFdvcmxkIQ"` instead of `"SGVsbG8sIFdvcmxkIQ=="`. To specify the URL convention, you can pass the appropriate option to our decoding and encoding functions: e.g., `simdutf::base64_to_binary(source, size, out, simdutf::base64_url)`.
2125
+
2126
+ When we encounter a character that is neither an ASCII space nor a base64 character (a garbage character), we detect an error. To tolerate 'garbage' characters, you can use `base64_default_accept_garbage` or `base64_url_accept_garbage` instead of `base64_default` or `base64_url`.
2127
+
2128
+ Thus we follow the convention of systems such as the Node or Bun JavaScript runtimes with respect to padding. The
2129
+ default base64 uses padding whereas the URL variant does not.
2130
+
2131
+ ```JavaScript
2132
+ > console.log(Buffer.from("Hello World").toString('base64'));
2133
+ SGVsbG8gV29ybGQ=
2134
+ undefined
2135
+ > console.log(Buffer.from("Hello World").toString('base64url'));
2136
+ SGVsbG8gV29ybGQ
2137
+ ```
2138
+
2139
+ This is justified as per [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648):
2140
+
2141
+ > The pad character "=" is typically percent-encoded when used in an URI, but if the data length is known implicitly, this can be avoided by skipping the padding; see section 3.2.
2142
+
2143
+ Nevertheless, some users may want to use padding with the URL variant
2144
+ and omit it with the default variant. These users can
2145
+ 'reverse' the convention by using `simdutf::base64_url | simdutf::base64_reverse_padding` or `simdutf::base64_default | simdutf::base64_reverse_padding`.
2146
+ For greater convenience, you may use `simdutf::base64_default_no_padding` and
2147
+ `simdutf::base64_url_with_padding`, as shorthands.
2148
+
2149
+ When decoding, by default we use a loose approach: the padding character may be omitted.
2150
+ Advanced users may use the `last_chunk_options` parameter to use either a `strict` approach,
2151
+ where precise padding must be used or an error is generated, or the `stop_before_partial`
2152
+ option which discards leftover base64 characters when the padding is not appropriate.
2153
+ The `stop_before_partial` option might be appropriate for streaming applications
2154
+ where you expect to get part of the base64 stream.
2155
+ The `strict` approach is useful if you want to have one-to-one correspondence between
2156
+ the base64 code and the binary data. If the default setting is used (`last_chunk_handling_options::loose`),
2157
+ then `"ZXhhZg=="`, `"ZXhhZg"`, `"ZXhhZh=="` all decode to the same binary content.
2158
+ If `last_chunk_options` is set to `last_chunk_handling_options::strict`, then
2159
+ decoding `"ZXhhZg=="` succeeds, but decoding `"ZXhhZg"` fails with `simdutf::error_code::BASE64_INPUT_REMAINDER` while `"ZXhhZh=="` fails with
2160
+ `simdutf::error_code::BASE64_EXTRA_BITS`. If `last_chunk_options` is set to `last_chunk_handling_options::stop_before_partial`,
2161
+ then decoding `"ZXhhZg"` decodes into `exa` (and `Zg` is left over).
2162
+
2163
+ The specification of our base64 functions is as follows:
2164
+
2165
+ ```cpp
2166
+
2167
+ // base64_options are used to specify the base64 encoding options.
2168
+ // ASCII spaces are ' ', '\t', '\n', '\r', '\f'
2169
+ // garbage characters are characters that are not part of the base64 alphabet nor ASCII spaces.
2170
+ using base64_options = uint64_t;
2171
+ enum base64_options : uint64_t {
2172
+ base64_default = 0, /* standard base64 format (with padding) */
2173
+ base64_url = 1, /* base64url format (no padding) */
2174
+ base64_default_no_padding =
2175
+ base64_default |
2176
+ base64_reverse_padding, /* standard base64 format without padding */
2177
+ base64_url_with_padding =
2178
+ base64_url | base64_reverse_padding, /* base64url with padding */
2179
+ base64_default_accept_garbage =
2180
+ 4, /* standard base64 format accepting garbage characters, the input stops with the first '=' if any */
2181
+ base64_url_accept_garbage =
2182
+ 5, /* base64url format accepting garbage characters, the input stops with the first '=' if any */
2183
+ base64_default_or_url =
2184
+ 8, /* standard/base64url hybrid format (only meaningful for decoding!) */
2185
+ base64_default_or_url_accept_garbage =
2186
+ 12, /* standard/base64url hybrid format accepting garbage characters
2187
+ (only meaningful for decoding!), the input stops with the first '=' if any */
2188
+ };
2189
+
2190
+ // last_chunk_handling_options are used to specify the handling of the last
2191
+ // chunk in base64 decoding.
2192
+ // https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
2193
+ enum last_chunk_handling_options : uint64_t {
2194
+ loose = 0, /* standard base64 format, decode partial final chunk */
2195
+ strict = 1, /* error when the last chunk is partial, 2 or 3 chars, and unpadded, or non-zero bit padding */
2196
+ stop_before_partial = 2, /* if the last chunk is partial , ignore it (no error) */
2197
+ only_full_chunks = 3 /* only decode full blocks (4 base64 characters, no padding) */
2198
+ };
2199
+
2200
+ /**
2201
+ * Provide the maximal binary length in bytes given the base64 input.
2202
+ * As long as the input does not contain ignorable characters (e.g., ASCII spaces
2203
+ * or linefeed characters), the result is exact. In particular, the function
2204
+ * checks for padding characters.
2205
+ *
2206
+ * The function is fast (constant time). It checks up to two characters at
2207
+ * the end of the string. The input is not otherwise validated or read.
2208
+ *
2209
+ * @param input the base64 input to process
2210
+ * @param length the length of the base64 input in bytes
2211
+ * @return maximal number of binary bytes
2212
+ */
2213
+ simdutf_warn_unused size_t maximal_binary_length_from_base64(const char * input, size_t length) noexcept;
2214
+
2215
+ /**
2216
+ * Provide the maximal binary length in bytes given the base64 input.
2217
+ * As long as the input does not contain ignorable characters (e.g., ASCII spaces
2218
+ * or linefeed characters), the result is exact. In particular, the function
2219
+ * checks for padding characters.
2220
+ *
2221
+ * The function is fast (constant time). It checks up to two characters at
2222
+ * the end of the string. The input is not otherwise validated or read.
2223
+ *
2224
+ * @param input the base64 input to process, in ASCII stored as 16-bit units
2225
+ * @param length the length of the base64 input in 16-bit units
2226
+ * @return maximal number of binary bytes
2227
+ */
2228
+ simdutf_warn_unused size_t maximal_binary_length_from_base64(const char16_t * input, size_t length) noexcept;
2229
+
2230
+ /**
2231
+ * Compute the binary length from a base64 input.
2232
+ * This function is useful for base64 inputs that may contain ASCII whitespaces
2233
+ * (such as line breaks). For such inputs, the result is exact, and for any
2234
+ * inputs the result can be used to size the output buffer passed to
2235
+ * `base64_to_binary`.
2236
+ *
2237
+ * The function ignores whitespace and does not require padding characters ('=').
2238
+ *
2239
+ * @param input the base64 input to process
2240
+ * @param length the length of the base64 input in bytes
2241
+ * @return number of binary bytes
2242
+ */
2243
+ simdutf_warn_unused size_t binary_length_from_base64(const char * input, size_t length) noexcept;
2244
+
2245
+ /**
2246
+ * Compute the binary length from a base64 input.
2247
+ * This function is useful for base64 inputs that may contain ASCII whitespaces
2248
+ * (such as line breaks). For such inputs, the result is exact, and for any
2249
+ * inputs the result can be used to size the output buffer passed to
2250
+ * `base64_to_binary`.
2251
+ *
2252
+ * The function ignores whitespace and does not require padding characters ('=').
2253
+ *
2254
+ * @param input the base64 input to process, in ASCII stored as 16-bit units
2255
+ * @param length the length of the base64 input in 16-bit units
2256
+ * @return number of binary bytes
2257
+ */
2258
+ simdutf_warn_unused size_t binary_length_from_base64(const char16_t * input, size_t length) noexcept;
2259
+
2260
+
2261
+ /**
2262
+ * Convert a base64 input to a binary output.
2263
+ *
2264
+ * This function follows the WHATWG forgiving-base64 format, which means that it
2265
+ * will ignore any ASCII spaces in the input. You may provide a padded input
2266
+ * (with one or two equal signs at the end) or an unpadded input (without any
2267
+ * equal signs at the end).
2268
+ *
2269
+ * See https://infra.spec.whatwg.org/#forgiving-base64-decode
2270
+ *
2271
+ * This function will fail in case of invalid input. When last_chunk_options = loose,
2272
+ * there are two possible reasons for failure: the input contains a number of
2273
+ * base64 characters that when divided by 4, leaves a single remainder character
2274
+ * (BASE64_INPUT_REMAINDER), or the input contains a character that is not a
2275
+ * valid base64 character (INVALID_BASE64_CHARACTER).
2276
+ *
2277
+ * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the
2278
+ * input where the invalid character was found. When the error is
2279
+ * BASE64_INPUT_REMAINDER, then r.count contains the number of bytes decoded.
2280
+ *
2281
+ * The default option (simdutf::base64_default) expects the characters `+` and
2282
+ * `/` as part of its alphabet. The URL option (simdutf::base64_url) expects the
2283
+ * characters `-` and `_` as part of its alphabet.
2284
+ *
2285
+ * The padding (`=`) is validated if present. There may be at most two padding
2286
+ * characters at the end of the input. If there are any padding characters, the
2287
+ * total number of characters (excluding spaces but including padding
2288
+ * characters) must be divisible by four.
2289
+ *
2290
+ * You should call this function with a buffer that is at least
2291
+ * maximal_binary_length_from_base64(input, length) bytes long. If you fail to
2292
+ * provide that much space, the function may cause a buffer overflow.
2293
+ *
2294
+ * Advanced users may want to tailor how the last chunk is handled. By default,
2295
+ * we use a loose (forgiving) approach but we also support a strict approach
2296
+ * as well as a stop_before_partial approach, as per the following proposal:
2297
+ *
2298
+ * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
2299
+ *
2300
+ * @param input the base64 string to process
2301
+ * @param length the length of the string in bytes
2302
+ * @param output the pointer to a buffer that can hold the conversion
2303
+ * result (should be at least maximal_binary_length_from_base64(input, length)
2304
+ * bytes long).
2305
+ * @param options the base64 options to use, usually base64_default or
2306
+ * base64_url, and base64_default by default.
2307
+ * @param last_chunk_options the last chunk handling options,
2308
+ * last_chunk_handling_options::loose by default
2309
+ * but can also be last_chunk_handling_options::strict or
2310
+ * last_chunk_handling_options::stop_before_partial.
2311
+ * @return a result pair struct (of type simdutf::result containing the two
2312
+ * fields error and count) with an error code and either position of the error
2313
+ * (in the input in bytes) if any, or the number of bytes written if successful.
2314
+ */
2315
+ simdutf_warn_unused result
2316
+ base64_to_binary(const char *input, size_t length, char *output,
2317
+ base64_options options = base64_default,
2318
+ last_chunk_handling_options last_chunk_options = loose) noexcept;
2319
+
2320
+ /**
2321
+ * Provide the base64 length in bytes given the length of a binary input.
2322
+ *
2323
+ * @param length the length of the input in bytes
2324
+ * @param options the base64 options to use, can be base64_default or base64_url, is base64_default by default.
2325
+ * @return number of base64 bytes
2326
+ */
2327
+ simdutf_warn_unused size_t base64_length_from_binary(size_t length, base64_options options = base64_default) noexcept;
2328
+
2329
+
2330
+ /**
2331
+ * Provide the base64 length in bytes given the length of a binary input,
2332
+ * taking into account line breaks.
2333
+ *
2334
+ * @param length the length of the input in bytes
2335
+ * @param options the base64 options to use, can be base64_default or base64_url, is base64_default by default.
2336
+ * @param line_length the length of lines, must be at least 4 (otherwise it is interpreted as 4),
2337
+ * @return number of base64 bytes
2338
+ */
2339
+ simdutf_warn_unused size_t
2340
+ base64_length_from_binary_with_lines(size_t length, base64_options options, size_t line_length) noexcept;
2341
+
2342
+
2343
+ /**
2344
+ * Convert a binary input to a base64 output.
2345
+ *
2346
+ * The default option (simdutf::base64_default) uses the characters `+` and `/` as part of its alphabet.
2347
+ * Further, it adds padding (`=`) at the end of the output to ensure that the output length is a multiple of four.
2348
+ *
2349
+ * The URL option (simdutf::base64_url) uses the characters `-` and `_` as part of its alphabet. No padding
2350
+ * is added at the end of the output.
2351
+ *
2352
+ * This function always succeeds.
2353
+ *
2354
+ * @param input the binary to process
2355
+ * @param length the length of the input in bytes
2356
+ * @param output the pointer to a buffer that can hold the conversion result (should be at least base64_length_from_binary(length) bytes long)
2357
+ * @param options the base64 options to use, can be base64_default or base64_url, is base64_default by default.
2358
+ * @return number of written bytes, will be equal to base64_length_from_binary(length, options)
2359
+ */
2360
+ size_t binary_to_base64(const char * input, size_t length, char* output, base64_options options = base64_default) noexcept;
2361
+
2362
+
2363
+ /**
2364
+ * Convert a binary input to a base64 output with line breaks.
2365
+ *
2366
+ * The default option (simdutf::base64_default) uses the characters `+` and `/`
2367
+ * as part of its alphabet. Further, it adds padding (`=`) at the end of the
2368
+ * output to ensure that the output length is a multiple of four.
2369
+ *
2370
+ * The URL option (simdutf::base64_url) uses the characters `-` and `_` as part
2371
+ * of its alphabet. No padding is added at the end of the output.
2372
+ *
2373
+ * This function always succeeds.
2374
+ *
2375
+ * The default line length is default_line_length (76)
2376
+ *
2377
+ * @param input the binary to process
2378
+ * @param length the length of the input in bytes
2379
+ * @param output the pointer to a buffer that can hold the conversion
2380
+ * result (should be at least base64_length_from_binary_with_lines(length, options, line_length) bytes long)
2381
+ * @param line_length the length of lines, must be at least 4 (otherwise it is interpreted as 4),
2382
+ * @param options the base64 options to use, can be base64_default or
2383
+ * base64_url, is base64_default by default.
2384
+ * @return number of written bytes, will be equal to
2385
+ * base64_length_from_binary_with_lines(length, options)
2386
+ */
2387
+ size_t binary_to_base64_with_lines(const char *input, size_t length, char *output,
2388
+ size_t line_length = simdutf::default_line_length,
2389
+ base64_options options = base64_default) noexcept;
2390
+
2391
+ /**
2392
+ * Convert a base64 input to a binary output.
2393
+ *
2394
+ * This function follows the WHATWG forgiving-base64 format, which means that it will
2395
+ * ignore any ASCII spaces in the input. You may provide a padded input (with one or two
2396
+ * equal signs at the end) or an unpadded input (without any equal signs at the end).
2397
+ *
2398
+ * See https://infra.spec.whatwg.org/#forgiving-base64-decode
2399
+ *
2400
+ * This function will fail in case of invalid input. When last_chunk_options = loose,
2401
+ * there are two possible reasons for failure: the input contains a number of
2402
+ * base64 characters that when divided by 4, leaves a single remainder character
2403
+ * (BASE64_INPUT_REMAINDER), or the input contains a character that is not a
2404
+ * valid base64 character (INVALID_BASE64_CHARACTER).
2405
+ *
2406
+ * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the input
2407
+ * where the invalid character was found. When the error is BASE64_INPUT_REMAINDER, then
2408
+ * r.count contains the number of bytes decoded.
2409
+ *
2410
+ * You should call this function with a buffer that is at least maximal_binary_length_from_base64(input, length) bytes long.
2411
+ * If you fail to provide that much space, the function may cause a buffer overflow.
2412
+ *
2413
+ * Advanced users may want to tailor how the last chunk is handled. By default,
2414
+ * we use a loose (forgiving) approach but we also support a strict approach
2415
+ * as well as a stop_before_partial approach, as per the following proposal:
2416
+ *
2417
+ * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
2418
+ *
2419
+ * @param input the base64 string to process, in ASCII stored as 16-bit units
2420
+ * @param length the length of the string in 16-bit units
2421
+ * @param output the pointer to a buffer that can hold the conversion result (should be at least maximal_binary_length_from_base64(input, length) bytes long).
2422
+ * @param options the base64 options to use, can be base64_default or base64_url, is base64_default by default.
2423
+ * @param last_chunk_options the last chunk handling options,
2424
+ * last_chunk_handling_options::loose by default
2425
+ * but can also be last_chunk_handling_options::strict or
2426
+ * last_chunk_handling_options::stop_before_partial.
2427
+ * @return a result pair struct (of type simdutf::result containing the two fields error and count) with an error code and either position of the INVALID_BASE64_CHARACTER error (in the input in units) if any, or the number of bytes written if successful.
2428
+ */
2429
+ simdutf_warn_unused result base64_to_binary(const char16_t * input, size_t length, char* output, base64_options options = base64_default, last_chunk_handling_options last_chunk_options =
2430
+ last_chunk_handling_options::loose) noexcept;
2431
+
2432
+
2433
+ /**
2434
+ * Convert a base64 input to a binary output.
2435
+ *
2436
+ * This function follows the WHATWG forgiving-base64 format, which means that it
2437
+ * will ignore any ASCII spaces in the input. You may provide a padded input
2438
+ * (with one or two equal signs at the end) or an unpadded input (without any
2439
+ * equal signs at the end).
2440
+ *
2441
+ * See https://infra.spec.whatwg.org/#forgiving-base64-decode
2442
+ *
2443
+ * This function will fail in case of invalid input. When last_chunk_options =
2444
+ * loose, there are three possible reasons for failure: the input contains a
2445
+ * number of base64 characters that when divided by 4, leaves a single remainder
2446
+ * character (BASE64_INPUT_REMAINDER), the input contains a character that is
2447
+ * not a valid base64 character (INVALID_BASE64_CHARACTER), or the output buffer
2448
+ * is too small (OUTPUT_BUFFER_TOO_SMALL).
2449
+ *
2450
+ * When OUTPUT_BUFFER_TOO_SMALL, we return both the number of bytes written
2451
+ * and the number of units processed, see description of the parameters and
2452
+ * returned value.
2453
+ *
2454
+ * When the error is INVALID_BASE64_CHARACTER, r.count contains the index in the
2455
+ * input where the invalid character was found. When the error is
2456
+ * BASE64_INPUT_REMAINDER, then r.count contains the number of bytes decoded.
2457
+ *
2458
+ * The default option (simdutf::base64_default) expects the characters `+` and
2459
+ * `/` as part of its alphabet. The URL option (simdutf::base64_url) expects the
2460
+ * characters `-` and `_` as part of its alphabet.
2461
+ *
2462
+ * The padding (`=`) is validated if present. There may be at most two padding
2463
+ * characters at the end of the input. If there are any padding characters, the
2464
+ * total number of characters (excluding spaces but including padding
2465
+ * characters) must be divisible by four.
2466
+ *
2467
+ * The INVALID_BASE64_CHARACTER cases are considered fatal and you are expected
2468
+ * to discard the output unless the parameter decode_up_to_bad_char is set to
2469
+ * true. In that case, the function will decode up to the first invalid character.
2470
+ * Extra padding characters ('=') are considered invalid characters.
2471
+ *
2472
+ * Advanced users may want to tailor how the last chunk is handled. By default,
2473
+ * we use a loose (forgiving) approach but we also support a strict approach
2474
+ * as well as a stop_before_partial approach, as per the following proposal:
2475
+ *
2476
+ * https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
2477
+ *
2478
+ * @param input the base64 string to process, in ASCII stored as 8-bit
2479
+ * or 16-bit units
2480
+ * @param length the length of the string in 8-bit or 16-bit units.
2481
+ * @param output the pointer to a buffer that can hold the conversion
2482
+ * result.
2483
+ * @param outlen the number of bytes that can be written in the output
2484
+ * buffer. Upon return, it is modified to reflect how many bytes were written.
2485
+ * @param options the base64 options to use, can be base64_default or
2486
+ * base64_url, is base64_default by default.
2487
+ * @param last_chunk_options the last chunk handling options,
2488
+ * last_chunk_handling_options::loose by default
2489
+ * but can also be last_chunk_handling_options::strict or
2490
+ * last_chunk_handling_options::stop_before_partial.
2491
+ * @param decode_up_to_bad_char if true, the function will decode up to the
2492
+ * first invalid character. By default (false), it is assumed that the output
2493
+ * buffer is to be discarded. When there are multiple errors in the input,
2494
+ * using decode_up_to_bad_char might trigger a different error.
2495
+ * @return a result pair struct (of type simdutf::result containing the two
2496
+ * fields error and count) with an error code and position of the
2497
+ * INVALID_BASE64_CHARACTER error (in the input in units) if any, or the number
2498
+ * of units processed if successful.
2499
+ */
2500
+ simdutf_warn_unused result base64_to_binary_safe(const char * input, size_t length, char* output, size_t& outlen, base64_options options = base64_default,
2501
+ last_chunk_handling_options last_chunk_options = loose,
2502
+ bool decode_up_to_bad_char = false) noexcept;
2503
+ simdutf_warn_unused result base64_to_binary_safe(const char16_t * input, size_t length, char* output, size_t& outlen, base64_options options = base64_default,
2504
+ last_chunk_handling_options last_chunk_options = loose,
2505
+ bool decode_up_to_bad_char = false) noexcept;
2506
+ ```
2507
+
2508
+ ## Find
2509
+
2510
+ The C++ standard library provides `std::find` for locating a character in a string, but its performance can be suboptimal on modern hardware. To address this, we introduce `simdutf::find`, a high-performance alternative optimized for recent processors using SIMD instructions. It operates on raw pointers (`char` or `char16_t`) for maximum efficiency.
2511
+
2512
+
2513
+
2514
+ ```cpp
2515
+ std::string input = "abc";
2516
+
2517
+ const char* result =
2518
+ simdutf::find(input.data(), input.data() + input.size(), 'c');
2519
+ // result should point at the letter 'c'
2520
+ ```
2521
+
2522
+ The `simdutf::find` interface is straightforward and efficient.
2523
+
2524
+
2525
+ ```cpp
2526
+ /**
2527
+ * Find the first occurrence of a character in a string. If the character is
2528
+ * not found, return a pointer to the end of the string.
2529
+ * @param start the start of the string
2530
+ * @param end the end of the string
2531
+ * @param character the character to find
2532
+ * @return a pointer to the first occurrence of the character in the string,
2533
+ * or a pointer to the end of the string if the character is not found.
2534
+ *
2535
+ */
2536
+ simdutf_warn_unused const char *find(const char *start, const char *end,
2537
+ char character) noexcept;
2538
+ simdutf_warn_unused const char16_t *find(const char16_t *start, const char16_t *end,
2539
+ char16_t character) noexcept;
2540
+ ```
2541
+
2542
+ ## C++20 and std::span usage in simdutf
2543
+
2544
+ If you are compiling with C++20 or later, span support is enabled. This allows you to use simdutf in a safer and more expressive way, without manually handling pointers and sizes.
2545
+
2546
+ The span interface is easy to use. If you have a container like `std::vector` or `std::array`, you can pass the container directly. If you have a pointer and a size, construct a `std::span` and pass it.
2547
+ When dealing with ranges of bytes (like `char`), anything that has a `std::span-like` interface (has appropriate `data()` and `size()` member functions) is accepted. Ranges of larger types are accepted as `std::span` arguments.
2548
+
2549
+ ### Example
2550
+
2551
+ Suppose you want to convert a UTF-16 string to UTF-8:
2552
+
2553
+ ```cpp
2554
+ #include <simdutf.h>
2555
+ #include <vector>
2556
+ #include <span>
2557
+ #include <string>
2558
+
2559
+ std::u16string utf16_input = u"Bonjour le monde";
2560
+ std::vector<char> utf8_output(64); // ensure sufficient size
2561
+
2562
+ // Use std::span for input and output
2563
+ size_t written = simdutf::convert_utf16_to_utf8_safe(utf16_input, utf8_output);
2564
+ ```
2565
+
2566
+
2567
+ ### Note
2568
+
2569
+ - You are still responsible for providing a sufficiently large output buffer, just as with the pointer/size API.
2570
+
2571
+ ## C++23 and constexpr support
2572
+
2573
+ If using C++23 or newer, it is possible to use the functions in the public api at compile time, with the following exceptions:
2574
+
2575
+ * `atomic_binary_to_base64`
2576
+ * `atomic_base64_to_binary_safe`
2577
+
2578
+ The following functions are also not constexpr but expected to be so in a future version:
2579
+
2580
+ * `autodetect_encoding`
2581
+ * `detect_encodings`
2582
+
2583
+ Here is an example:
2584
+
2585
+ ```cpp
2586
+ constexpr std::span s(u8"My favourite dish is köttbullar!");
2587
+ static_assert(!simdutf::validate_ascii(s));
2588
+ static_assert(simdutf::validate_utf8(s));
2589
+ static_assert(s.size() != simdutf::latin1_length_from_utf8(s));
2590
+ ```
2591
+
2592
+ To use the constexpr functionality, your have to go through the span overloads.
2593
+
2594
+ The constexpr functionality is tested with `static_assert` in the unit tests which is handy - if it compiled, the unit tests passed!
2595
+
2596
+ ### Note - the constexpr support is experimental!
2597
+
2598
+ The constexpr support is implemented with functions that are already tested and proven. There were however
2599
+ modifications made to make it usable at constexpr time. Also, when in a constexpr context, the functions are not invoked exactly
2600
+ as during normal dynamic invocation. For this reason, there might have slipped in subtle bugs and the constexpr
2601
+ support is considered experimental. Please report any bugs you encounter!
2602
+
2603
+ ## The sutf command-line tool
2604
+
2605
+ We also provide a command-line tool which can be build as follows:
2606
+ ```
2607
+ cmake -B build && cmake --build build --target sutf
2608
+ ```
2609
+ This command builds the executable in `./build/tool/` under most platforms. The sutf tool enables the user to easily transcode files from one encoding to another directly from the command line.
2610
+ The usage is similar to [iconv](https://www.gnu.org/software/libiconv/) (see `sutf --help` for more details). The sutf command-line tool relies on the simdutf library functions for fast transcoding of supported
2611
+ formats (UTF-8, UTF-16LE, UTF-16BE and UTF-32). If iconv is found on the system and simdutf does not support a conversion, the sutf tool falls back on iconv: a message lets the user know if iconv is available
2612
+ during compilation. The following is an example of transcoding two input files to an output file, from UTF-8 to UTF-16LE:
2613
+ ```
2614
+ sutf -f UTF-8 -t UTF-16LE -o output_file.txt first_input_file.txt second_input_file.txt
2615
+ ```
2616
+
2617
+ ## Manual implementation selection
2618
+
2619
+ When compiling the library for x64 processors, we build several implementations of each functions. At runtime, the best
2620
+ implementation is picked automatically. Advanced users may want to pick a particular implementation, thus bypassing our
2621
+ runtime detection. It is possible and even relatively convenient to do so. The following C++ program checks all the available
2622
+ implementation, and selects one as the default:
2623
+
2624
+ ```cpp
2625
+ #include "simdutf.h"
2626
+ #include <cstdlib>
2627
+ #include <iostream>
2628
+ #include <string>
2629
+
2630
+ int main() {
2631
+ // This is just a demonstration, not actual testing required.
2632
+ std::string source = "La vie est belle.";
2633
+ std::string chosen_implementation;
2634
+ for (auto &implementation : simdutf::get_available_implementations()) {
2635
+ if (!implementation->supported_by_runtime_system()) {
2636
+ continue;
2637
+ }
2638
+ bool validutf8 = implementation->validate_utf8(source.c_str(), source.size());
2639
+ if (!validutf8) {
2640
+ return EXIT_FAILURE;
2641
+ }
2642
+ std::cout << implementation->name() << ": " << implementation->description()
2643
+ << std::endl;
2644
+ chosen_implementation = implementation->name();
2645
+ }
2646
+ auto my_implementation =
2647
+ simdutf::get_available_implementations()[chosen_implementation];
2648
+ if (!my_implementation) {
2649
+ return EXIT_FAILURE;
2650
+ }
2651
+ if (!my_implementation->supported_by_runtime_system()) {
2652
+ return EXIT_FAILURE;
2653
+ }
2654
+ simdutf::get_active_implementation() = my_implementation;
2655
+ bool validutf8 = simdutf::validate_utf8(source.c_str(), source.size());
2656
+ if (!validutf8) {
2657
+ return EXIT_FAILURE;
2658
+ }
2659
+ if (simdutf::get_active_implementation()->name() != chosen_implementation) {
2660
+ return EXIT_FAILURE;
2661
+ }
2662
+ std::cout << "I have manually selected: " << simdutf::get_active_implementation()->name() << std::endl;
2663
+ return EXIT_SUCCESS;
2664
+ }
2665
+ ```
2666
+
2667
+
2668
+
2669
+ ## Benchmarks
2670
+
2671
+ To run benchmarks, build the project with benchmarks enabled. Our default benchmarks are in the `benchmark` command. You can get help on its
2672
+ usage by first building it and then calling it with the `--help` flag.
2673
+ E.g., under Linux you may do the following:
2674
+
2675
+ ```shell
2676
+ cmake -B build -D SIMDUTF_BENCHMARKS=ON
2677
+ cmake --build build
2678
+ ./build/benchmarks/benchmark --help
2679
+ ```
2680
+
2681
+
2682
+ The standard benchmark tool `benchmark` provides comprehensive transcoding benchmarks between different encodings. It supports various procedures like converting UTF-8 to UTF-16, UTF-16 to UTF-8, and more. You can list available procedures with `--procedures`, run specific benchmarks, or use filters to select particular tests. For example, to benchmark UTF-8 to UTF-16 conversion on a file, use `./build/benchmarks/benchmark --procedure utf8_to_utf16 file.txt`. It outputs detailed performance metrics including throughput in GB/s and CPU cycles.
2683
+
2684
+ Since ICU is so common and popular, we assume that you may have it already on your system. When
2685
+ it is not found, it is simply omitted from the benchmarks. Thus, to benchmark against ICU, make
2686
+ sure you have ICU installed on your machine and that cmake can find it. For macOS, you may
2687
+ install it with brew using `brew install icu4c`. If you have ICU on your system but cmake cannot
2688
+ find it, you may need to provide cmake with a path to ICU, such as `ICU_ROOT=/usr/local/opt/icu4c cmake -B build`.
2689
+
2690
+
2691
+ ### Base64 benchmarks
2692
+
2693
+ We also have a base64 benchmark tool (`benchmark_base64`).
2694
+
2695
+ ```shell
2696
+ ./build/benchmarks/base64/benchmark_base64 --help
2697
+ ```
2698
+
2699
+ E.g., to run base64 decoding benchmarks on DNS data (short inputs), do
2700
+
2701
+ ```shell
2702
+ ./build/benchmarks/base64/benchmark_base64 -d pathto/base64data/dns/*.txt
2703
+ ```
2704
+
2705
+ where pathto/base64data should contain the path to a clone of
2706
+ the repository https://github.com/lemire/base64data.
2707
+
2708
+
2709
+ ### Short input benchmarks
2710
+
2711
+ To run short benchmarks on various SIMDUTF functions with incremental input sizes, use `shortbench`:
2712
+
2713
+ ```shell
2714
+ ./build/benchmarks/shortbench --help
2715
+ ./build/benchmarks/shortbench --list
2716
+ ./build/benchmarks/shortbench --function validate_utf8 # validate a zero buffer
2717
+ ./build/benchmarks/shortbench --function validate_utf8 README.md
2718
+ ./build/benchmarks/shortbench --function utf8_length_from_latin1 --max-size 256 somefile.txt
2719
+ ```
2720
+
2721
+ This will benchmark the selected function on the input file, testing sizes from 1 byte up to the specified max size (default 128), and output a table with timing and performance metrics.
2722
+
2723
+
2724
+ ## Thread safety
2725
+
2726
+ We built simdutf with thread safety in mind. The simdutf library is single-threaded throughout.
2727
+ The CPU detection, which runs the first time parsing is attempted and switches to the fastest parser for your CPU, is transparent and thread-safe. Our runtime dispatching is based on global objects that are instantiated at the beginning of the main thread and may be discarded at the end of the main thread. If you have multiple threads running and some threads use the library while the main thread is cleaning up resources, you may encounter issues. If you expect such problems, you may consider using [std::quick_exit](https://en.cppreference.com/w/cpp/utility/program/quick_exit).
2728
+
2729
+
2730
+ ## References
2731
+
2732
+ * Robert Clausecker, Daniel Lemire, [Transcoding Unicode Characters with AVX-512 Instructions](https://arxiv.org/abs/2212.05098), Software: Practice and Experience 53 (12), 2023.
2733
+ * Daniel Lemire, Wojciech Muła, [Transcoding Billions of Unicode Characters per Second with SIMD Instructions](https://arxiv.org/abs/2109.10433), Software: Practice and Experience 52 (2), 2022.
2734
+ * John Keiser, Daniel Lemire, [Validating UTF-8 In Less Than One Instruction Per Byte](https://arxiv.org/abs/2010.03090), Software: Practice and Experience 51 (5), 2021.
2735
+ * Wojciech Muła, Daniel Lemire, [Base64 encoding and decoding at almost the speed of a memory copy](https://arxiv.org/abs/1910.05109), Software: Practice and Experience 50 (2), 2020.
2736
+ * Wojciech Muła, Daniel Lemire, [Faster Base64 Encoding and Decoding using AVX2 Instructions](https://arxiv.org/abs/1704.00605), ACM Transactions on the Web 12 (3), 2018.
2737
+
2738
+
2739
+ ## Citing this work
2740
+
2741
+ If you use this library in your research, please cite our work:
2742
+
2743
+ ```bibtex
2744
+ @misc{simdutf,
2745
+ title={The simdutf library: {Unicode} validation and transcoding at billions of characters per second},
2746
+ author={Daniel Lemire and Wojciech Mu{\l}a and Paul Dreik and others},
2747
+ year={2021},
2748
+ note={\url{https://github.com/simdutf/simdutf}}
2749
+ }
2750
+ ```
2751
+
2752
+ ## C wrapper (C11 or better)
2753
+
2754
+ *This is currently experimental. We are committed to maintaining the C API but there might be issues with
2755
+ our implementation.*
2756
+
2757
+ We provide a thin C API that wraps the C++ `simdutf` library. It is intended
2758
+ for applications that prefer or require a plain C interface. The `simdutf_c.h`
2759
+ defines the interface.
2760
+
2761
+ The C API exposes functions for validation, transcoding, size estimation, `find` helpers,
2762
+ and Base64 encode/decode helpers. Results are returned using the `simdutf_result` struct
2763
+ which contains an `error_code` field and additional fields when relevant.
2764
+
2765
+ We provide a simple C demo using the C wrapper at `amalgamation_demo.c`.
2766
+ It shows validating UTF-8, converting UTF-8 to UTF-16LE and back, and checking the round-trip.
2767
+ Refer to `singleheader/README.md` for instructions. Note that the simdutf library requires
2768
+ a C++ standard library (e.g., libstdc++, libc++) at runtime, either statically or dynamically linked.
2769
+
2770
+ Note: The C API is currently not aware of amalgamation with limited features. It expects the full simdutf library.
2771
+
2772
+
2773
+ ## Stars
2774
+
2775
+ [![Star History Chart](https://api.star-history.com/svg?repos=simdutf/simdutf&type=Date)](https://www.star-history.com/#simdutf/simdutf&Date)
2776
+
2777
+ ## License
2778
+
2779
+ This code is made available under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) as well as the MIT license. As a user, you can pick the license you prefer.
2780
+
2781
+ We include a few competitive solutions under the benchmarks/competition directory. They are provided for
2782
+ research purposes only.