Skip to content

Commit 2222b8c

Browse files
authored
Merge pull request #182 from itchyny/fix-implode-replacement-character
Fix implode/0 to emit replacement characters on invalid input numbers
2 parents 2186777 + 4524d40 commit 2222b8c

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/intrinsic/string.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ pub(crate) fn implode(value: Value) -> Result<Value> {
8181
let s = s
8282
.iter()
8383
.map(|c| match c {
84-
Value::Number(c) => {
85-
let c = c
86-
.to_u32()
87-
.ok_or(QueryExecutionError::InvalidNumberAsChar(*c))?;
88-
let c = char::try_from(c)
89-
.map_err(|_| QueryExecutionError::InvalidNumberAsChar(c.into()))?;
90-
Ok(c)
91-
}
84+
Value::Number(c) => Ok(c
85+
.to_u32()
86+
.and_then(|c| char::try_from(c).ok())
87+
.unwrap_or(char::REPLACEMENT_CHARACTER)),
9288
_ => Err(QueryExecutionError::InvalidArgType("implode", c.clone())),
9389
})
9490
.collect::<Result<String>>()?;

src/vm/error.rs

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub enum QueryExecutionError {
6262
InvalidAsBase64(#[from] base64::DecodeError),
6363
#[error("Invalid as a UTF-8-encoded byte array")]
6464
InvalidUTF8Bytes(#[from] std::string::FromUtf8Error),
65-
#[error("Invalid as a unicode scalar value `{0:}`")]
66-
InvalidNumberAsChar(Number),
6765
#[error("utf8bytelength is applied to non-string value`{0:?}`")]
6866
InvalidUTF8ByteLength(Value),
6967
#[error("{0:?} was called invalidly with arg `{1:?}`")]

tests/from_manual/functions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,19 @@ test!(
12031203
"#
12041204
);
12051205

1206+
test!(
1207+
implode2,
1208+
r#"
1209+
implode
1210+
"#,
1211+
r#"
1212+
[-1, 0, 1, 1114111, 1114112]
1213+
"#,
1214+
r#"
1215+
"\ufffd\u0000\u0001\udbff\udfff\ufffd"
1216+
"#
1217+
);
1218+
12061219
test!(
12071220
split1,
12081221
r#"

0 commit comments

Comments
 (0)