Skip to content

Commit

Permalink
Merge pull request #182 from itchyny/fix-implode-replacement-character
Browse files Browse the repository at this point in the history
Fix implode/0 to emit replacement characters on invalid input numbers
  • Loading branch information
MiSawa authored Mar 5, 2024
2 parents 2186777 + 4524d40 commit 2222b8c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
12 changes: 4 additions & 8 deletions src/intrinsic/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ pub(crate) fn implode(value: Value) -> Result<Value> {
let s = s
.iter()
.map(|c| match c {
Value::Number(c) => {
let c = c
.to_u32()
.ok_or(QueryExecutionError::InvalidNumberAsChar(*c))?;
let c = char::try_from(c)
.map_err(|_| QueryExecutionError::InvalidNumberAsChar(c.into()))?;
Ok(c)
}
Value::Number(c) => Ok(c
.to_u32()
.and_then(|c| char::try_from(c).ok())
.unwrap_or(char::REPLACEMENT_CHARACTER)),
_ => Err(QueryExecutionError::InvalidArgType("implode", c.clone())),
})
.collect::<Result<String>>()?;
Expand Down
2 changes: 0 additions & 2 deletions src/vm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ pub enum QueryExecutionError {
InvalidAsBase64(#[from] base64::DecodeError),
#[error("Invalid as a UTF-8-encoded byte array")]
InvalidUTF8Bytes(#[from] std::string::FromUtf8Error),
#[error("Invalid as a unicode scalar value `{0:}`")]
InvalidNumberAsChar(Number),
#[error("utf8bytelength is applied to non-string value`{0:?}`")]
InvalidUTF8ByteLength(Value),
#[error("{0:?} was called invalidly with arg `{1:?}`")]
Expand Down
13 changes: 13 additions & 0 deletions tests/from_manual/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,19 @@ test!(
"#
);

test!(
implode2,
r#"
implode
"#,
r#"
[-1, 0, 1, 1114111, 1114112]
"#,
r#"
"\ufffd\u0000\u0001\udbff\udfff\ufffd"
"#
);

test!(
split1,
r#"
Expand Down

0 comments on commit 2222b8c

Please sign in to comment.