From 2e3a3994727fefba68f5f42f86566b0b5ba46106 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Tue, 1 Oct 2024 22:57:05 +0800 Subject: [PATCH] remove UTF16/UTF32 support, as they will cause confuse --- vlib/encoding/iconv/iconv.v | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vlib/encoding/iconv/iconv.v b/vlib/encoding/iconv/iconv.v index db43dad62b8322..8d1e30c8436689 100644 --- a/vlib/encoding/iconv/iconv.v +++ b/vlib/encoding/iconv/iconv.v @@ -6,12 +6,20 @@ import os // vstring_to_encoding convert V string `str` to `tocode` encoding string // tips: use `iconv --list` check for supported encodings pub fn vstring_to_encoding(str string, tocode string) ![]u8 { + encoding_name := tocode.to_upper() + if encoding_name in ['UTF16', 'UTF32', 'UTF-16', 'UTF-32']! { + return error('please use UTF16LE/UTF16BE/UTF32LE/UTF32BE instead') + } return conv(tocode, 'UTF-8', str.str, str.len) } // encoding_to_vstring converts the given `bytes` using `fromcode` encoding, to a V string (encoded with UTF-8) // tips: use `iconv --list` check for supported encodings pub fn encoding_to_vstring(bytes []u8, fromcode string) !string { + encoding_name := fromcode.to_upper() + if encoding_name in ['UTF16', 'UTF32', 'UTF-16', 'UTF-32']! { + return error('please use UTF16LE/UTF16BE/UTF32LE/UTF32BE instead') + } mut dst := conv('UTF-8', fromcode, bytes.data, bytes.len)! dst << [u8(0)] // Windows: add tail zero, to build a vstring return unsafe { cstring_to_vstring(dst.data) } @@ -101,6 +109,10 @@ pub fn remove_utf_string_with_bom(src []u8, utf_type string) []u8 { // write_file_encoding write_file convert `text` into `encoding` and writes to a file with the given `path`. If `path` already exists, it will be overwritten. // For `encoding` in UTF8/UTF16/UTF32, if `bom` is true, then a BOM header will write to the file. pub fn write_file_encoding(path string, text string, encoding string, bom bool) ! { + encoding_name := encoding.to_upper() + if encoding_name in ['UTF16', 'UTF32', 'UTF-16', 'UTF-32']! { + return error('please use UTF16LE/UTF16BE/UTF32LE/UTF32BE instead') + } encoding_bytes := vstring_to_encoding(text, encoding)! if bom && encoding.to_upper().starts_with('UTF') { encoding_bom_bytes := create_utf_string_with_bom(encoding_bytes, encoding) @@ -112,6 +124,10 @@ pub fn write_file_encoding(path string, text string, encoding string, bom bool) // read_file_encoding reads the file in `path` with `encoding` and returns the contents pub fn read_file_encoding(path string, encoding string) !string { + encoding_name := encoding.to_upper() + if encoding_name in ['UTF16', 'UTF32', 'UTF-16', 'UTF-32']! { + return error('please use UTF16LE/UTF16BE/UTF32LE/UTF32BE instead') + } encoding_bytes := os.read_file_array[u8](path) println(encoding_bytes) encoding_without_bom_bytes := remove_utf_string_with_bom(encoding_bytes, encoding)