Skip to content

Commit

Permalink
Replace encoding with encoding_rs (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzak committed Jun 2, 2023
1 parent 3c47e64 commit 3110856
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 305 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ required-features = ["sql-browser-smol"]
[dependencies]
enumflags2 = "0.7"
byteorder = "1.0"
encoding = "0.2"
encoding_rs = "0.8"
once_cell = "1.3"
thiserror = "1.0"
bytes = "1.0"
Expand Down
18 changes: 13 additions & 5 deletions src/tds/codec/column_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::{
};
use bytes::BufMut;
pub(crate) use bytes_mut_with_type_info::BytesMutWithTypeInfo;
use encoding::EncoderTrap;
use std::borrow::{BorrowMut, Cow};
use uuid::Uuid;

Expand Down Expand Up @@ -302,10 +301,19 @@ impl<'a> Encode<BytesMutWithTypeInfo<'a>> for ColumnData<'a> {
|| vlc.r#type() == VarLenType::BigVarChar =>
{
if let Some(str) = opt {
let encoder = vlc.collation().as_ref().unwrap().encoding()?;
let bytes = encoder
.encode(str.as_ref(), EncoderTrap::Strict)
.map_err(crate::Error::Encoding)?;
let mut encoder = vlc.collation().as_ref().unwrap().encoding()?.new_encoder();
let len = encoder
.max_buffer_length_from_utf8_without_replacement(str.len())
.unwrap();
let mut bytes = Vec::with_capacity(len);
let (res, _) = encoder.encode_from_utf8_to_vec_without_replacement(
str.as_ref(),
&mut bytes,
true,
);
if let encoding_rs::EncoderResult::Unmappable(_) = res {
return Err(crate::Error::Encoding("unrepresentable character".into()));
}

if bytes.len() > vlc.len() {
return Err(crate::Error::BulkInput(
Expand Down
8 changes: 4 additions & 4 deletions src/tds/codec/column_data/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::borrow::Cow;

use byteorder::{ByteOrder, LittleEndian};
use encoding::DecoderTrap;

use crate::{error::Error, sql_read_bytes::SqlReadBytes, tds::Collation, VarLenType};

Expand All @@ -24,9 +23,10 @@ where
let collation = collation.as_ref().unwrap();
let encoder = collation.encoding()?;

let s: String = encoder
.decode(buf.as_ref(), DecoderTrap::Strict)
.map_err(Error::Encoding)?;
let s = encoder
.decode_without_bom_handling_and_without_replacement(buf.as_ref())
.ok_or_else(|| Error::Encoding("invalid sequence".into()))?
.to_string();

Ok(Some(s.into()))
}
Expand Down
7 changes: 3 additions & 4 deletions src/tds/codec/column_data/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use encoding::DecoderTrap;

use crate::{error::Error, sql_read_bytes::SqlReadBytes, tds::Collation, ColumnData};

pub(crate) async fn decode<R>(
Expand Down Expand Up @@ -34,8 +32,9 @@ where
}

encoder
.decode(buf.as_ref(), DecoderTrap::Strict)
.map_err(Error::Encoding)?
.decode_without_bom_handling_and_without_replacement(buf.as_ref())
.ok_or_else(|| Error::Encoding("invalid sequence".into()))?
.to_string()
}
// NTEXT
None => {
Expand Down

0 comments on commit 3110856

Please sign in to comment.