@@ -375,10 +375,7 @@ pub fn try_read_nil(input: &str) -> IResult<&str, Value> {
375
375
/// Example Successes:
376
376
/// "this is pretty straightforward" => Value::String("this is pretty straightforward")
377
377
pub fn try_read_string ( input : & str ) -> IResult < & str , Value > {
378
- named ! ( quotation<& str , & str >, preceded!( consume_clojure_whitespaces_parser, tag!( "\" " ) ) ) ;
379
-
380
- let ( rest_input, _) = quotation ( input) ?;
381
-
378
+ // Convert escaped characters like \n to their actual counterparts -- like an actual newline
382
379
named ! ( escaped_string_parser<& str , String >, escaped_transform!( take_till1!( |ch| { ch == '\\' || ch == '\"' } ) , '\\' , alt!(
383
380
tag!( "t" ) => { |_| "\t " } |
384
381
tag!( "b" ) => { |_| "\x08 " } |
@@ -389,16 +386,18 @@ pub fn try_read_string(input: &str) -> IResult<&str, Value> {
389
386
tag!( "\" " ) => { |_| "\" " } |
390
387
tag!( "\\ " ) => { |_| "\\ " }
391
388
) ) ) ;
389
+
390
+ named ! ( empty_string_parser <& str , String >, map!( tag!( "\" \" " ) , |v| String :: from( "" ) ) ) ;
392
391
393
392
named ! (
394
393
string_parser<& str , String >,
395
- map !(
396
- terminated! ( escaped_string_parser, tag( "\" " ) ) ,
397
- |v| String :: from ( v )
398
- )
394
+ alt !(
395
+ delimited! ( tag ( " \" " ) , escaped_string_parser, tag( "\" " ) ) |
396
+ // Base case; empty string
397
+ empty_string_parser )
399
398
) ;
400
399
401
- to_value_parser ( string_parser) ( rest_input )
400
+ to_value_parser ( string_parser) ( input )
402
401
}
403
402
404
403
pub fn try_read_pattern ( input : & str ) -> IResult < & str , Value > {
@@ -782,6 +781,31 @@ mod tests {
782
781
) ;
783
782
}
784
783
784
+
785
+ #[ test]
786
+ fn try_read_string_empty ( ) {
787
+ assert_eq ! (
788
+ Value :: String ( String :: from( "" ) ) ,
789
+ try_read( "\" \" " ) . ok( ) . unwrap( ) . 1
790
+ ) ;
791
+ }
792
+
793
+ #[ test]
794
+ fn try_read_string_escaped_quotes ( ) {
795
+ assert_eq ! (
796
+ Value :: String ( String :: from( "\" \" c c caf \" fadsg" ) ) ,
797
+ try_read( r#""\" \" c c caf \" fadsg""# ) . ok( ) . unwrap( ) . 1
798
+ ) ;
799
+ }
800
+
801
+ #[ test]
802
+ fn try_read_string_newlines ( ) {
803
+ assert_eq ! (
804
+ Value :: String ( String :: from( "\n fadsg \n " ) ) ,
805
+ try_read( r#""\n fadsg \n""# ) . ok( ) . unwrap( ) . 1
806
+ ) ;
807
+ }
808
+
785
809
#[ test]
786
810
fn try_read_int_test ( ) {
787
811
assert_eq ! ( Value :: I32 ( 1 ) , try_read( "1 " ) . ok( ) . unwrap( ) . 1 ) ;
0 commit comments