@@ -972,6 +972,10 @@ impl CheetahString {
972972
973973 #[ inline]
974974 fn push_str_internal ( & mut self , string : & str ) {
975+ if string. is_empty ( ) {
976+ return ;
977+ }
978+
975979 match & mut self . inner {
976980 InnerString :: Inline { len, data } => {
977981 let total_len = * len as usize + string. len ( ) ;
@@ -1039,6 +1043,10 @@ impl CheetahString {
10391043 /// ```
10401044 #[ inline]
10411045 pub fn reserve ( & mut self , additional : usize ) {
1046+ if additional == 0 {
1047+ return ;
1048+ }
1049+
10421050 match & mut self . inner {
10431051 InnerString :: Inline { len, .. } if * len as usize + additional <= INLINE_CAPACITY => {
10441052 return ;
@@ -1202,28 +1210,9 @@ impl Add<&str> for CheetahString {
12021210 /// assert_eq!(result, "Hello World");
12031211 /// ```
12041212 #[ inline]
1205- fn add ( self , rhs : & str ) -> Self :: Output {
1206- let total_len = self . len ( ) + rhs. len ( ) ;
1207-
1208- // Fast path: result fits in inline storage
1209- if total_len <= INLINE_CAPACITY {
1210- let mut data = [ 0u8 ; INLINE_CAPACITY ] ;
1211- let self_bytes = self . as_bytes ( ) ;
1212- data[ ..self_bytes. len ( ) ] . copy_from_slice ( self_bytes) ;
1213- data[ self_bytes. len ( ) ..total_len] . copy_from_slice ( rhs. as_bytes ( ) ) ;
1214- return CheetahString {
1215- inner : InnerString :: Inline {
1216- len : total_len as u8 ,
1217- data,
1218- } ,
1219- } ;
1220- }
1221-
1222- // Slow path: allocate for long result
1223- let mut result = String :: with_capacity ( total_len) ;
1224- result. push_str ( self . as_str ( ) ) ;
1225- result. push_str ( rhs) ;
1226- CheetahString :: from_string ( result)
1213+ fn add ( mut self , rhs : & str ) -> Self :: Output {
1214+ self . push_str_internal ( rhs) ;
1215+ self
12271216 }
12281217}
12291218
@@ -1243,28 +1232,9 @@ impl Add<&CheetahString> for CheetahString {
12431232 /// assert_eq!(result, "Hello World");
12441233 /// ```
12451234 #[ inline]
1246- fn add ( self , rhs : & CheetahString ) -> Self :: Output {
1247- let total_len = self . len ( ) + rhs. len ( ) ;
1248-
1249- // Fast path: result fits in inline storage
1250- if total_len <= INLINE_CAPACITY {
1251- let mut data = [ 0u8 ; INLINE_CAPACITY ] ;
1252- let self_bytes = self . as_bytes ( ) ;
1253- data[ ..self_bytes. len ( ) ] . copy_from_slice ( self_bytes) ;
1254- data[ self_bytes. len ( ) ..total_len] . copy_from_slice ( rhs. as_bytes ( ) ) ;
1255- return CheetahString {
1256- inner : InnerString :: Inline {
1257- len : total_len as u8 ,
1258- data,
1259- } ,
1260- } ;
1261- }
1262-
1263- // Slow path: allocate for long result
1264- let mut result = String :: with_capacity ( total_len) ;
1265- result. push_str ( self . as_str ( ) ) ;
1266- result. push_str ( rhs. as_str ( ) ) ;
1267- CheetahString :: from_string ( result)
1235+ fn add ( mut self , rhs : & CheetahString ) -> Self :: Output {
1236+ self . push_str_internal ( rhs. as_str ( ) ) ;
1237+ self
12681238 }
12691239}
12701240
@@ -1283,28 +1253,13 @@ impl Add<String> for CheetahString {
12831253 /// assert_eq!(result, "Hello World");
12841254 /// ```
12851255 #[ inline]
1286- fn add ( self , rhs : String ) -> Self :: Output {
1287- let total_len = self . len ( ) + rhs. len ( ) ;
1288-
1289- // Fast path: result fits in inline storage
1290- if total_len <= INLINE_CAPACITY {
1291- let mut data = [ 0u8 ; INLINE_CAPACITY ] ;
1292- let self_bytes = self . as_bytes ( ) ;
1293- data[ ..self_bytes. len ( ) ] . copy_from_slice ( self_bytes) ;
1294- data[ self_bytes. len ( ) ..total_len] . copy_from_slice ( rhs. as_bytes ( ) ) ;
1295- return CheetahString {
1296- inner : InnerString :: Inline {
1297- len : total_len as u8 ,
1298- data,
1299- } ,
1300- } ;
1256+ fn add ( mut self , rhs : String ) -> Self :: Output {
1257+ if self . is_empty ( ) {
1258+ return CheetahString :: from_string ( rhs) ;
13011259 }
13021260
1303- // Slow path: allocate for long result
1304- let mut result = String :: with_capacity ( total_len) ;
1305- result. push_str ( self . as_str ( ) ) ;
1306- result. push_str ( & rhs) ;
1307- CheetahString :: from_string ( result)
1261+ self . push_str_internal ( & rhs) ;
1262+ self
13081263 }
13091264}
13101265
0 commit comments