@@ -79,13 +79,9 @@ pub struct Event {
79
79
}
80
80
81
81
const LOGIFY_MAX_CHARS : usize = 100 ;
82
- fn logify ( bytes : & [ u8 ] ) -> & str {
82
+ fn logify ( bytes : & [ u8 ] ) -> String {
83
83
let stringified = from_utf8 ( bytes) . unwrap_or ( "<bad utf8>" ) ;
84
- if stringified. len ( ) <= LOGIFY_MAX_CHARS {
85
- stringified
86
- } else {
87
- & stringified[ ..LOGIFY_MAX_CHARS - 1 ]
88
- }
84
+ stringified. chars ( ) . take ( LOGIFY_MAX_CHARS ) . collect ( )
89
85
}
90
86
91
87
fn parse_field ( line : & [ u8 ] ) -> Result < Option < ( & str , & str ) > > {
@@ -367,6 +363,8 @@ impl EventParser {
367
363
368
364
#[ cfg( test) ]
369
365
mod tests {
366
+ use std:: str:: FromStr ;
367
+
370
368
use super :: { Error :: * , * } ;
371
369
use proptest:: proptest;
372
370
use test_case:: test_case;
@@ -388,6 +386,19 @@ mod tests {
388
386
}
389
387
}
390
388
389
+ #[ test]
390
+ fn test_logify_handles_code_point_boundaries ( ) {
391
+ let phase = String :: from_str (
392
+ "这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。" ,
393
+ )
394
+ . expect ( "Invalid sample string" ) ;
395
+
396
+ let input: & [ u8 ] = phase. as_bytes ( ) ;
397
+ let result = logify ( input) ;
398
+
399
+ assert ! ( result == "这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如此。这是一条很长的消息,最初导致我们的代码出现恐慌。我希望情况不再如" ) ;
400
+ }
401
+
391
402
#[ test]
392
403
fn test_parse_field_invalid ( ) {
393
404
assert ! ( parse_field( b"" ) . is_err( ) ) ;
0 commit comments