@@ -1405,7 +1405,7 @@ protected static void WriteRawParameter(IXdrWriter xdr, DbField field)
14051405 else
14061406 {
14071407 var svalue = field . DbValue . GetString ( ) ;
1408- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . EnumerateRunes ( ) . Count ( ) > field . CharCount )
1408+ if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . CountRunes ( ) > field . CharCount )
14091409 {
14101410 throw IscException . ForErrorCodes ( new [ ] { IscCodes . isc_arith_except , IscCodes . isc_string_truncation } ) ;
14111411 }
@@ -1445,7 +1445,7 @@ protected static void WriteRawParameter(IXdrWriter xdr, DbField field)
14451445 else
14461446 {
14471447 var svalue = field . DbValue . GetString ( ) ;
1448- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . EnumerateRunes ( ) . Count ( ) > field . CharCount )
1448+ if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . CountRunes ( ) > field . CharCount )
14491449 {
14501450 throw IscException . ForErrorCodes ( [ IscCodes . isc_arith_except , IscCodes . isc_string_truncation ] ) ;
14511451 }
@@ -1579,7 +1579,7 @@ protected static async ValueTask WriteRawParameterAsync(IXdrWriter xdr, DbField
15791579 else
15801580 {
15811581 var svalue = await field . DbValue . GetStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
1582- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . EnumerateRunes ( ) . Count ( ) > field . CharCount )
1582+ if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . CountRunes ( ) > field . CharCount )
15831583 {
15841584 throw IscException . ForErrorCodes ( [ IscCodes . isc_arith_except , IscCodes . isc_string_truncation ] ) ;
15851585 }
@@ -1612,7 +1612,7 @@ protected static async ValueTask WriteRawParameterAsync(IXdrWriter xdr, DbField
16121612 else
16131613 {
16141614 var svalue = await field . DbValue . GetStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
1615- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . EnumerateRunes ( ) . Count ( ) > field . CharCount )
1615+ if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 && svalue . CountRunes ( ) > field . CharCount )
16161616 {
16171617 throw IscException . ForErrorCodes ( [ IscCodes . isc_arith_except , IscCodes . isc_string_truncation ] ) ;
16181618 }
@@ -1738,16 +1738,7 @@ protected object ReadRawValue(IXdrReader xdr, DbField field)
17381738 else
17391739 {
17401740 var s = xdr . ReadString ( innerCharset , field . Length ) ;
1741- var runes = s . EnumerateRunesToChars ( ) . ToList ( ) ;
1742- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 &&
1743- runes . Count > field . CharCount )
1744- {
1745- return new string ( [ .. runes . Take ( field . CharCount ) . SelectMany ( x => x ) ] ) ;
1746- }
1747- else
1748- {
1749- return s ;
1750- }
1741+ return TruncateStringByRuneCount ( s , field ) ;
17511742 }
17521743
17531744 case DbDataType . VarChar :
@@ -1836,16 +1827,7 @@ protected async ValueTask<object> ReadRawValueAsync(IXdrReader xdr, DbField fiel
18361827 else
18371828 {
18381829 var s = await xdr . ReadStringAsync ( innerCharset , field . Length , cancellationToken ) . ConfigureAwait ( false ) ;
1839- var runes = s . EnumerateRunesToChars ( ) . ToList ( ) ;
1840- if ( ( field . Length % field . Charset . BytesPerCharacter ) == 0 &&
1841- runes . Count > field . CharCount )
1842- {
1843- return new string ( [ .. runes . Take ( field . CharCount ) . SelectMany ( x => x ) ] ) ;
1844- }
1845- else
1846- {
1847- return s ;
1848- }
1830+ return TruncateStringByRuneCount ( s , field ) ;
18491831 }
18501832
18511833 case DbDataType . VarChar :
@@ -2002,6 +1984,22 @@ protected virtual async ValueTask<DbValue[]> ReadRowAsync(CancellationToken canc
20021984 return row ;
20031985 }
20041986
1987+ private static string TruncateStringByRuneCount ( string s , DbField field )
1988+ {
1989+ if ( ( field . Length % field . Charset . BytesPerCharacter ) != 0 )
1990+ {
1991+ return s ;
1992+ }
1993+
1994+ var runeCount = s . CountRunes ( ) ;
1995+ if ( runeCount <= field . CharCount )
1996+ {
1997+ return s ;
1998+ }
1999+
2000+ return new string ( s . TruncateStringToRuneCount ( field . CharCount ) ) ;
2001+ }
2002+
20052003 #endregion
20062004
20072005 #region Protected Internal Methods
0 commit comments