@@ -79,13 +79,9 @@ pub struct Event {
7979}
8080
8181const LOGIFY_MAX_CHARS : usize = 100 ;
82- fn logify ( bytes : & [ u8 ] ) -> & str {
82+ fn logify ( bytes : & [ u8 ] ) -> String {
8383 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 ( )
8985}
9086
9187fn parse_field ( line : & [ u8 ] ) -> Result < Option < ( & str , & str ) > > {
@@ -367,6 +363,8 @@ impl EventParser {
367363
368364#[ cfg( test) ]
369365mod tests {
366+ use std:: str:: FromStr ;
367+
370368 use super :: { Error :: * , * } ;
371369 use proptest:: proptest;
372370 use test_case:: test_case;
@@ -388,6 +386,19 @@ mod tests {
388386 }
389387 }
390388
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+
391402 #[ test]
392403 fn test_parse_field_invalid ( ) {
393404 assert ! ( parse_field( b"" ) . is_err( ) ) ;
0 commit comments