File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change @@ -610,23 +610,25 @@ xy_str_delete_suffix (const char *str, const char *suffix)
610610static char *
611611xy_str_strip (const char * str )
612612{
613- char * new = xy_strdup (str );
613+ if (!str )
614+ return xy_strdup ("" );
614615
615- while (strchr ("\n\r\v\t\f " , new [0 ]))
616- {
617- new += 1 ;
618- }
616+ const char * start = str ;
617+ while (* start && strchr ("\n\r\v\t\f " , * start ))
618+ start ++ ;
619619
620- size_t len = strlen (new );
620+ if ('\0' == * start )
621+ return xy_strdup ("" );
621622
622- char * last = new + len - 1 ;
623+ const char * end = start + strlen (start ) - 1 ;
624+ while (end >= start && strchr ("\n\r\v\t\f " , * end ))
625+ end -- ;
623626
624- while (strchr ("\n\r\v\t\f " , * last ))
625- {
626- * last = '\0' ;
627- last -= 1 ;
628- }
629- return new ;
627+ size_t len = (size_t ) (end - start + 1 );
628+ char * ret = xy_malloc0 (len + 1 );
629+ memcpy (ret , start , len );
630+ ret [len ] = '\0' ;
631+ return ret ;
630632}
631633
632634typedef struct
You can’t perform that action at this time.
0 commit comments