|
417 | 417 |
|
418 | 418 | unescape_string(s::String) = print_to_string(print_unescaped, s)
|
419 | 419 |
|
| 420 | +## checking UTF-8 validity ## |
| 421 | + |
| 422 | +is_valid_utf8(s::ByteString) = is_valid_utf8(s.data) |
| 423 | +is_valid_utf8(a::Array{Uint8,1}) = |
| 424 | + bool(ccall(:u8_isvalid, Int32, (Ptr{Uint8}, Int32), a, length(a))) |
| 425 | +check_utf8(s::ByteString) = is_valid_utf8(s) ? s : error("invalid UTF-8 sequence") |
| 426 | + |
420 | 427 | ## string interpolation parsing ##
|
421 | 428 |
|
422 | 429 | function interp_parse(str::String, unescape::Function)
|
@@ -450,14 +457,14 @@ function interp_parse(str::String, unescape::Function)
|
450 | 457 | !isa(strs[1],String) ? expr(:call,:string,strs[1]) : strs[1]
|
451 | 458 | end
|
452 | 459 |
|
453 |
| -interp_parse(str::String) = interp_parse(str, unescape_string) |
| 460 | +interp_parse(str::String) = interp_parse(str, s->check_utf8(unescape_string(s))) |
454 | 461 |
|
455 | 462 | ## core string macros ##
|
456 | 463 |
|
457 | 464 | macro str(s); interp_parse(s); end
|
458 | 465 | macro S_str(s); interp_parse(s); end
|
459 |
| -macro I_str(s); interp_parse(s, unbackslash); end |
460 |
| -macro E_str(s); unescape_string(s); end |
| 466 | +macro I_str(s); interp_parse(s, s->unbackslash(s)); end |
| 467 | +macro E_str(s); check_utf8(unescape_string(s)); end |
461 | 468 |
|
462 | 469 | ## shell-like command parsing ##
|
463 | 470 |
|
|
0 commit comments