@@ -392,6 +392,51 @@ static char ToAscii(int b) {
392392 }
393393 }
394394
395+ // new in CPython 3.8
396+ public string hex ( [ NotNone ] string sep , int bytes_per_sep = 1 ) {
397+ if ( sep . Length != 1 ) throw PythonOps . ValueError ( $ "{ nameof ( sep ) } must be length 1") ;
398+ return ToHex ( _bytes . AsSpan ( ) , sep [ 0 ] , bytes_per_sep ) ;
399+ }
400+
401+ // new in CPython 3.8
402+ public string hex ( [ BytesLike , NotNone ] IList < byte > sep , int bytes_per_sep = 1 ) {
403+ if ( sep . Count != 1 ) throw PythonOps . ValueError ( $ "{ nameof ( sep ) } must be length 1") ;
404+ return ToHex ( _bytes . AsSpan ( ) , ( char ) sep [ 0 ] , bytes_per_sep ) ;
405+ }
406+
407+ internal static string ToHex ( ReadOnlySpan < byte > bytes , char sep , int bytes_per_sep ) {
408+ if ( sep >= 0x80 ) throw PythonOps . ValueError ( $ "{ nameof ( sep ) } must be ASCII") ;
409+ if ( bytes . Length == 0 ) return string . Empty ;
410+ if ( bytes_per_sep == 0 ) return ToHex ( bytes ) ;
411+
412+ int sepLoc ;
413+ if ( bytes_per_sep < 0 ) {
414+ bytes_per_sep = - bytes_per_sep ;
415+ sepLoc = 0 ;
416+ } else {
417+ sepLoc = bytes_per_sep - bytes . Length % bytes_per_sep ;
418+ if ( sepLoc == bytes_per_sep ) sepLoc = 0 ;
419+ }
420+
421+ var builder = new StringBuilder ( bytes . Length * 2 + ( bytes . Length - 1 ) / bytes_per_sep ) ;
422+ foreach ( var b in bytes ) {
423+ if ( sepLoc == bytes_per_sep ) {
424+ builder . Append ( sep ) ;
425+ sepLoc = 1 ;
426+ } else {
427+ sepLoc ++ ;
428+ }
429+ builder . Append ( ToAscii ( b >> 4 ) ) ;
430+ builder . Append ( ToAscii ( b & 0xf ) ) ;
431+ }
432+ Debug . Assert ( builder . Length == bytes . Length * 2 + ( bytes . Length - 1 ) / bytes_per_sep ) ;
433+ return builder . ToString ( ) ;
434+
435+ static char ToAscii ( int b ) {
436+ return ( char ) ( b < 10 ? '0' + b : 'a' + ( b - 10 ) ) ;
437+ }
438+ }
439+
395440 public int index ( [ BytesLike , NotNone ] IList < byte > sub )
396441 => index ( sub , 0 , _bytes . Length ) ;
397442
@@ -435,6 +480,9 @@ public int index(BigInteger @byte, object? start, object? end)
435480
436481 public bool isalpha ( ) => _bytes . IsLetter ( ) ;
437482
483+ // new in Python 3.7
484+ public bool isascii ( ) => _bytes . IsAscii ( ) ;
485+
438486 public bool isdigit ( ) => _bytes . IsDigit ( ) ;
439487
440488 public bool islower ( ) => _bytes . IsLower ( ) ;
0 commit comments