Skip to content

Commit

Permalink
remove UTF16/UTF32 support, as they will cause confuse
Browse files Browse the repository at this point in the history
  • Loading branch information
kbkpbot committed Oct 1, 2024
1 parent d7294d4 commit 2e3a399
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions vlib/encoding/iconv/iconv.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2e3a399

Please sign in to comment.