Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the characteristic that the trim() and trim_end() functions do not remove the \0 character. #620

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions core/src/value/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl PrimitiveValue {
{
match self {
PrimitiveValue::Str(s) => {
s.trim()
s.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseIntegerSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -919,7 +919,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) if !s.is_empty() => s[0]
.trim()
.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseIntegerSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::Empty => Ok(Vec::new()),
PrimitiveValue::Str(s) => {
let out = s.trim().parse().context(ParseIntegerSnafu).map_err(|err| {
let out = s.trim_end_matches([' ', '\u{0}']).parse().context(ParseIntegerSnafu).map_err(|err| {
ConvertValueError {
requested: "integer",
original: self.value_type(),
Expand All @@ -1090,7 +1090,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => {
s.iter()
.map(|v| {
v.trim().parse().context(ParseIntegerSnafu).map_err(|err| {
v.trim_end_matches([' ', '\u{0}']).parse().context(ParseIntegerSnafu).map_err(|err| {
ConvertValueError {
requested: "integer",
original: self.value_type(),
Expand Down Expand Up @@ -1256,7 +1256,7 @@ impl PrimitiveValue {
pub fn to_float32(&self) -> Result<f32, ConvertValueError> {
match self {
PrimitiveValue::Str(s) => {
s.trim()
s.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -1266,7 +1266,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) if !s.is_empty() => s[0]
.trim()
.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -1426,7 +1426,7 @@ impl PrimitiveValue {
PrimitiveValue::Empty => Ok(Vec::new()),
PrimitiveValue::Str(s) => {
let out =
s.trim()
s.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -1439,7 +1439,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => s
.iter()
.map(|v| {
v.trim()
v.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -1622,7 +1622,7 @@ impl PrimitiveValue {
pub fn to_float64(&self) -> Result<f64, ConvertValueError> {
match self {
PrimitiveValue::Str(s) => {
s.trim()
s.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -1632,7 +1632,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) if !s.is_empty() => s[0]
.trim()
.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -1791,7 +1791,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::Str(s) => {
let out =
s.trim()
s.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -1804,7 +1804,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => s
.iter()
.map(|v| {
v.trim()
v.trim_end_matches([' ', '\u{0}'])
.parse()
.context(ParseFloatSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2012,23 +2012,23 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::deserialize::parse_date(s.as_bytes())
PrimitiveValue::Str(s) => super::deserialize::parse_date(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
requested: "NaiveDate",
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => {
super::deserialize::parse_date(s.first().map(|s| s.as_bytes()).unwrap_or(&[]))
super::deserialize::parse_date(s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]))
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
requested: "NaiveDate",
original: self.value_type(),
cause: Some(Box::from(err)),
})
}
PrimitiveValue::U8(bytes) => super::deserialize::parse_date(bytes)
PrimitiveValue::U8(bytes) => super::deserialize::parse_date(trim_last_whitespace(bytes))
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
requested: "NaiveDate",
Expand Down Expand Up @@ -2104,7 +2104,7 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::deserialize::parse_date(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::deserialize::parse_date(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|date| vec![date])
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2114,7 +2114,7 @@ impl PrimitiveValue {
}),
PrimitiveValue::Strs(s) => s
.into_iter()
.map(|s| super::deserialize::parse_date(s.trim_end().as_bytes()))
.map(|s| super::deserialize::parse_date(s.trim_end_matches([' ', '\u{0}']).as_bytes()))
.collect::<Result<Vec<_>, _>>()
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2201,7 +2201,7 @@ impl PrimitiveValue {
pub fn to_date(&self) -> Result<DicomDate, ConvertValueError> {
match self {
PrimitiveValue::Date(d) if !d.is_empty() => Ok(d[0]),
PrimitiveValue::Str(s) => super::deserialize::parse_date_partial(s.as_bytes())
PrimitiveValue::Str(s) => super::deserialize::parse_date_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _)| date)
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2210,7 +2210,7 @@ impl PrimitiveValue {
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => super::deserialize::parse_date_partial(
s.first().map(|s| s.as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.map(|(date, _)| date)
.context(ParseDateSnafu)
Expand All @@ -2219,7 +2219,7 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::U8(bytes) => super::deserialize::parse_date_partial(bytes)
PrimitiveValue::U8(bytes) => super::deserialize::parse_date_partial(trim_last_whitespace(bytes))
.map(|(date, _)| date)
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2262,7 +2262,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::Date(d) => Ok(d.to_vec()),
PrimitiveValue::Str(s) => {
super::deserialize::parse_date_partial(s.trim_end().as_bytes())
super::deserialize::parse_date_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _)| vec![date])
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2274,7 +2274,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => s
.into_iter()
.map(|s| {
super::deserialize::parse_date_partial(s.trim_end().as_bytes())
super::deserialize::parse_date_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| date)
})
.collect::<Result<Vec<_>, _>>()
Expand Down Expand Up @@ -2353,7 +2353,7 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::deserialize::parse_time(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::deserialize::parse_time(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| date)
.context(ParseTimeSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2362,7 +2362,7 @@ impl PrimitiveValue {
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => super::deserialize::parse_time(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.map(|(date, _rest)| date)
.context(ParseTimeSnafu)
Expand Down Expand Up @@ -2450,7 +2450,7 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::deserialize::parse_time(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::deserialize::parse_time(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| vec![date])
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2461,7 +2461,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => s
.into_iter()
.map(|s| {
super::deserialize::parse_time(s.trim_end().as_bytes())
super::deserialize::parse_time(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| date)
})
.collect::<Result<Vec<_>, _>>()
Expand Down Expand Up @@ -2566,7 +2566,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::Time(t) if !t.is_empty() => Ok(t[0]),
PrimitiveValue::Str(s) => {
super::deserialize::parse_time_partial(s.trim_end().as_bytes())
super::deserialize::parse_time_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| date)
.context(ParseTimeSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2576,7 +2576,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) => super::deserialize::parse_time_partial(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.map(|(date, _rest)| date)
.context(ParseTimeSnafu)
Expand Down Expand Up @@ -2646,7 +2646,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::Time(t) => Ok(t.to_vec()),
PrimitiveValue::Str(s) => {
super::deserialize::parse_time_partial(s.trim_end().as_bytes())
super::deserialize::parse_time_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| vec![date])
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2658,7 +2658,7 @@ impl PrimitiveValue {
PrimitiveValue::Strs(s) => s
.into_iter()
.map(|s| {
super::deserialize::parse_time_partial(s.trim_end().as_bytes())
super::deserialize::parse_time_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|(date, _rest)| date)
})
.collect::<Result<Vec<_>, _>>()
Expand Down Expand Up @@ -2772,7 +2772,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::DateTime(v) if !v.is_empty() => Ok(v[0]),
PrimitiveValue::Str(s) => {
super::deserialize::parse_datetime_partial(s.trim_end().as_bytes())
super::deserialize::parse_datetime_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseDateTimeSnafu)
.map_err(|err| ConvertValueError {
requested: "DicomDateTime",
Expand All @@ -2781,7 +2781,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) => super::deserialize::parse_datetime_partial(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.context(ParseDateTimeSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2812,7 +2812,7 @@ impl PrimitiveValue {
match self {
PrimitiveValue::DateTime(v) => Ok(v.to_vec()),
PrimitiveValue::Str(s) => {
super::deserialize::parse_datetime_partial(s.trim_end().as_bytes())
super::deserialize::parse_datetime_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.map(|date| vec![date])
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand All @@ -2823,7 +2823,7 @@ impl PrimitiveValue {
}
PrimitiveValue::Strs(s) => s
.into_iter()
.map(|s| super::deserialize::parse_datetime_partial(s.trim_end().as_bytes()))
.map(|s| super::deserialize::parse_datetime_partial(s.trim_end_matches([' ', '\u{0}']).as_bytes()))
.collect::<Result<Vec<_>, _>>()
.context(ParseDateSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2895,15 +2895,15 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::range::parse_date_range(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::range::parse_date_range(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseDateRangeSnafu)
.map_err(|err| ConvertValueError {
requested: "DateRange",
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => super::range::parse_date_range(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.context(ParseDateRangeSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -2978,15 +2978,15 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::range::parse_time_range(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::range::parse_time_range(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseTimeRangeSnafu)
.map_err(|err| ConvertValueError {
requested: "TimeRange",
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => super::range::parse_time_range(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.context(ParseTimeRangeSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -3092,15 +3092,15 @@ impl PrimitiveValue {
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => super::range::parse_datetime_range(s.trim_end().as_bytes())
PrimitiveValue::Str(s) => super::range::parse_datetime_range(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseDateTimeRangeSnafu)
.map_err(|err| ConvertValueError {
requested: "DateTimeRange",
original: self.value_type(),
cause: Some(Box::from(err)),
}),
PrimitiveValue::Strs(s) => super::range::parse_datetime_range(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.context(ParseDateTimeRangeSnafu)
.map_err(|err| ConvertValueError {
Expand Down Expand Up @@ -3197,7 +3197,7 @@ impl PrimitiveValue {
cause: Some(Box::from(err)),
}),
PrimitiveValue::Str(s) => {
super::range::parse_datetime_range_custom::<T>(s.trim_end().as_bytes())
super::range::parse_datetime_range_custom::<T>(s.trim_end_matches([' ', '\u{0}']).as_bytes())
.context(ParseDateTimeRangeSnafu)
.map_err(|err| ConvertValueError {
requested: "DateTimeRange",
Expand All @@ -3206,7 +3206,7 @@ impl PrimitiveValue {
})
}
PrimitiveValue::Strs(s) => super::range::parse_datetime_range_custom::<T>(
s.first().map(|s| s.trim_end().as_bytes()).unwrap_or(&[]),
s.first().map(|s| s.trim_end_matches([' ', '\u{0}']).as_bytes()).unwrap_or(&[]),
)
.context(ParseDateTimeRangeSnafu)
.map_err(|err| ConvertValueError {
Expand Down