@@ -869,7 +869,7 @@ public virtual int GetByteCount(char[] chars)
869
869
[ Pure ]
870
870
public virtual int GetByteCount ( String s )
871
871
{
872
- if ( s == null )
872
+ if ( s == null )
873
873
throw new ArgumentNullException ( nameof ( s ) ) ;
874
874
Contract . EndContractBlock ( ) ;
875
875
@@ -884,6 +884,34 @@ public virtual int GetByteCount(String s)
884
884
[ Pure ]
885
885
public abstract int GetByteCount ( char [ ] chars , int index , int count ) ;
886
886
887
+ // Returns the number of bytes required to encode a string range.
888
+ //
889
+ [ Pure ]
890
+ public int GetByteCount ( string s , int index , int count )
891
+ {
892
+ if ( s == null )
893
+ throw new ArgumentNullException ( nameof ( s ) ,
894
+ Environment . GetResourceString ( "ArgumentNull_String" ) ) ;
895
+ if ( index < 0 )
896
+ throw new ArgumentOutOfRangeException ( nameof ( index ) ,
897
+ Environment . GetResourceString ( "ArgumentOutOfRange_NeedNonNegNum" ) ) ;
898
+ if ( count < 0 )
899
+ throw new ArgumentOutOfRangeException ( nameof ( count ) ,
900
+ Environment . GetResourceString ( "ArgumentOutOfRange_NeedNonNegNum" ) ) ;
901
+ if ( index > s . Length - count )
902
+ throw new ArgumentOutOfRangeException ( nameof ( index ) ,
903
+ Environment . GetResourceString ( "ArgumentOutOfRange_IndexCount" ) ) ;
904
+ Contract . EndContractBlock ( ) ;
905
+
906
+ unsafe
907
+ {
908
+ fixed ( char * pChar = s )
909
+ {
910
+ return GetByteCount ( pChar + index , count ) ;
911
+ }
912
+ }
913
+ }
914
+
887
915
// We expect this to be the workhorse for NLS encodings
888
916
// unfortunately for existing overrides, it has to call the [] version,
889
917
// which is really slow, so this method should be avoided if you're calling
@@ -978,10 +1006,49 @@ public virtual byte[] GetBytes(String s)
978
1006
return bytes ;
979
1007
}
980
1008
1009
+ // Returns a byte array containing the encoded representation of the given
1010
+ // string range.
1011
+ //
1012
+ [ Pure ]
1013
+ public byte [ ] GetBytes ( string s , int index , int count )
1014
+ {
1015
+ if ( s == null )
1016
+ throw new ArgumentNullException ( nameof ( s ) ,
1017
+ Environment . GetResourceString ( "ArgumentNull_String" ) ) ;
1018
+ if ( index < 0 )
1019
+ throw new ArgumentOutOfRangeException ( nameof ( index ) ,
1020
+ Environment . GetResourceString ( "ArgumentOutOfRange_NeedNonNegNum" ) ) ;
1021
+ if ( count < 0 )
1022
+ throw new ArgumentOutOfRangeException ( nameof ( count ) ,
1023
+ Environment . GetResourceString ( "ArgumentOutOfRange_NeedNonNegNum" ) ) ;
1024
+ if ( index > s . Length - count )
1025
+ throw new ArgumentOutOfRangeException ( nameof ( index ) ,
1026
+ Environment . GetResourceString ( "ArgumentOutOfRange_IndexCount" ) ) ;
1027
+ Contract . EndContractBlock ( ) ;
1028
+
1029
+ unsafe
1030
+ {
1031
+ fixed ( char * pChar = s )
1032
+ {
1033
+ int byteCount = GetByteCount ( pChar + index , count ) ;
1034
+ if ( byteCount == 0 )
1035
+ return Array . Empty < byte > ( ) ;
1036
+
1037
+ byte [ ] bytes = new byte [ byteCount ] ;
1038
+ fixed ( byte * pBytes = & bytes [ 0 ] )
1039
+ {
1040
+ int bytesReceived = GetBytes ( pChar + index , count , pBytes , byteCount ) ;
1041
+ Debug . Assert ( byteCount == bytesReceived ) ;
1042
+ }
1043
+ return bytes ;
1044
+ }
1045
+ }
1046
+ }
1047
+
981
1048
public virtual int GetBytes ( String s , int charIndex , int charCount ,
982
1049
byte [ ] bytes , int byteIndex )
983
1050
{
984
- if ( s == null )
1051
+ if ( s == null )
985
1052
throw new ArgumentNullException ( nameof ( s ) ) ;
986
1053
Contract . EndContractBlock ( ) ;
987
1054
return GetBytes ( s . ToCharArray ( ) , charIndex , charCount , bytes , byteIndex ) ;
0 commit comments