@@ -449,12 +449,86 @@ public void ToByteArray(byte[] destination, int offset)
449
449
destination [ offset + 11 ] = ( byte ) ( _c ) ;
450
450
}
451
451
452
+ #if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
453
+ /// <summary>
454
+ /// Converts the ObjectId to a byte buffer provided by the input span.
455
+ /// </summary>
456
+ /// <param name="destination">The destination byte span.</param>
457
+ /// <exception cref="ArgumentException">Not enough room in destination span.</exception>
458
+ public void ToByteSpan ( Span < byte > destination )
459
+ {
460
+ if ( destination . Length < 12 )
461
+ {
462
+ throw new ArgumentException ( "Not enough room in destination span." , "offset" ) ;
463
+ }
464
+
465
+ destination [ 0 ] = ( byte ) ( _a >> 24 ) ;
466
+ destination [ 1 ] = ( byte ) ( _a >> 16 ) ;
467
+ destination [ 2 ] = ( byte ) ( _a >> 8 ) ;
468
+ destination [ 3 ] = ( byte ) ( _a ) ;
469
+ destination [ 4 ] = ( byte ) ( _b >> 24 ) ;
470
+ destination [ 5 ] = ( byte ) ( _b >> 16 ) ;
471
+ destination [ 6 ] = ( byte ) ( _b >> 8 ) ;
472
+ destination [ 7 ] = ( byte ) ( _b ) ;
473
+ destination [ 8 ] = ( byte ) ( _c >> 24 ) ;
474
+ destination [ 9 ] = ( byte ) ( _c >> 16 ) ;
475
+ destination [ 10 ] = ( byte ) ( _c >> 8 ) ;
476
+ destination [ 11 ] = ( byte ) ( _c ) ;
477
+ }
478
+
479
+ /// <summary>
480
+ /// Fills a character span with the characters corresponding to the string representation of the value.
481
+ /// </summary>
482
+ /// <param name="destination">The span to fill the characters in.</param>
483
+ /// <exception cref="ArgumentException">Not enough room in destination span.</exception>
484
+ public void ToCharSpan ( Span < char > destination )
485
+ {
486
+ if ( destination . Length < 24 )
487
+ {
488
+ throw new ArgumentException ( "Not enough room in destination span." , "offset" ) ;
489
+ }
490
+
491
+ Span < byte > span = stackalloc byte [ 12 ] ;
492
+ ToByteSpan ( span ) ;
493
+ destination [ 0 ] = BsonUtils . ToHexChar ( span [ 3 ] >> 4 ) ;
494
+ destination [ 1 ] = BsonUtils . ToHexChar ( span [ 3 ] & 0xF ) ;
495
+ destination [ 2 ] = BsonUtils . ToHexChar ( span [ 2 ] >> 4 ) ;
496
+ destination [ 3 ] = BsonUtils . ToHexChar ( span [ 2 ] & 0xF ) ;
497
+ destination [ 4 ] = BsonUtils . ToHexChar ( span [ 1 ] >> 4 ) ;
498
+ destination [ 5 ] = BsonUtils . ToHexChar ( span [ 1 ] & 0xF ) ;
499
+ destination [ 6 ] = BsonUtils . ToHexChar ( span [ 0 ] >> 4 ) ;
500
+ destination [ 7 ] = BsonUtils . ToHexChar ( span [ 0 ] & 0xF ) ;
501
+ destination [ 8 ] = BsonUtils . ToHexChar ( span [ 7 ] >> 4 ) ;
502
+ destination [ 9 ] = BsonUtils . ToHexChar ( span [ 7 ] & 0xF ) ;
503
+ destination [ 10 ] = BsonUtils . ToHexChar ( span [ 6 ] >> 4 ) ;
504
+ destination [ 11 ] = BsonUtils . ToHexChar ( span [ 6 ] & 0xF ) ;
505
+ destination [ 12 ] = BsonUtils . ToHexChar ( span [ 5 ] >> 4 ) ;
506
+ destination [ 13 ] = BsonUtils . ToHexChar ( span [ 5 ] & 0xF ) ;
507
+ destination [ 14 ] = BsonUtils . ToHexChar ( span [ 4 ] >> 4 ) ;
508
+ destination [ 15 ] = BsonUtils . ToHexChar ( span [ 4 ] & 0xF ) ;
509
+ destination [ 16 ] = BsonUtils . ToHexChar ( span [ 11 ] >> 4 ) ;
510
+ destination [ 17 ] = BsonUtils . ToHexChar ( span [ 11 ] & 0xF ) ;
511
+ destination [ 18 ] = BsonUtils . ToHexChar ( span [ 10 ] >> 4 ) ;
512
+ destination [ 19 ] = BsonUtils . ToHexChar ( span [ 10 ] & 0xF ) ;
513
+ destination [ 20 ] = BsonUtils . ToHexChar ( span [ 9 ] >> 4 ) ;
514
+ destination [ 21 ] = BsonUtils . ToHexChar ( span [ 9 ] & 0xF ) ;
515
+ destination [ 22 ] = BsonUtils . ToHexChar ( span [ 8 ] >> 4 ) ;
516
+ destination [ 23 ] = BsonUtils . ToHexChar ( span [ 8 ] & 0xF ) ;
517
+ }
518
+ #endif
519
+
452
520
/// <summary>
453
521
/// Returns a string representation of the value.
454
522
/// </summary>
455
523
/// <returns>A string representation of the value.</returns>
456
524
public override string ToString ( )
457
525
{
526
+ #if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
527
+ return string . Create ( 24 , this , static ( span , input ) =>
528
+ {
529
+ input . ToCharSpan ( span ) ;
530
+ } ) ;
531
+ #else
458
532
var c = new char [ 24 ] ;
459
533
c [ 0 ] = BsonUtils . ToHexChar ( ( _a >> 28 ) & 0x0f ) ;
460
534
c [ 1 ] = BsonUtils . ToHexChar ( ( _a >> 24 ) & 0x0f ) ;
@@ -481,6 +555,7 @@ public override string ToString()
481
555
c [ 22 ] = BsonUtils . ToHexChar ( ( _c >> 4 ) & 0x0f ) ;
482
556
c [ 23 ] = BsonUtils . ToHexChar ( _c & 0x0f ) ;
483
557
return new string ( c ) ;
558
+ #endif
484
559
}
485
560
486
561
// explicit IConvertible implementation
0 commit comments