Repository navigation
string: prevent negative slice bounds and preserve surrogate pairs during truncation - #1658
Conversation
|
Good catch. The core bug is real and nasty: when The surrogate-pair handling is a legitimate secondary improvement (lone surrogates do break JSON/UTF-8/DB writes), and the One nit: the surrogate-pair test for Small, focused, in |
|
Good catch on the suffix length calculation — updated in ed6bdbe with index 20 so end = 21 lands directly after the high surrogate and exercises the back-off branch, asserting exact output and clean surrogate boundary preservation. |
What
truncateStringWithMessage,truncateString, andcapitalizenow handle negative slice boundaries and astral code points (surrogate pairs) safely.Why
In
truncateStringWithMessage, whenmaxLengthis smaller than or equal to the truncation marker (message):remove: 'END',maxLength - suffix.lengthbecame negative. In JavaScript,str.slice(0, negative)counts backwards from the end ofstr. For example, truncating a 100-character string withmaxLength: 20returned a 120-character string (slice(0, -8)returning 92 chars + 28-char suffix), expanding the string rather than truncating it.remove: 'MIDDLE',length = Math.floor((maxLength - middle.length) / 2)became negative.str.slice(0, length) + middle + str.slice(-length)sliced nearly the entire string on both sides of the marker, returning 219 characters for an input of 100 characters.remove: 'START', whenmaxLength <= prefix.length, the slice index exceededstr.length, prepending the oversized prefix to an empty string.In
truncateStringandtruncateStringWithMessage, slicing directly atmaxLengthor the message offset can cut an astral character (like an emoji or non-BMP symbol) in half, leaving a lone/dangling high surrogate (\uD800..\uDBFF). In UTF-8 encodings, JSON transmission, and database storage (such asdatabase.tsstoring truncated error messages), lone surrogates are either invalid, corrupt the text into replacement glyphs, or cause database write errors.In
capitalize,str.charAt(0)split surrogate pairs when the string started with an astral character.Fix
maxLength <= marker.lengthintruncateStringWithMessageacross all three removal strategies (END,START,MIDDLE), returning the marker sliced tomaxLength.0xD800..0xDBFF), or advance by one when starting after a low surrogate (0xDC00..0xDFFF), ensuring surrogate pairs are never split.Array.fromincapitalizeto iterate code points instead of UTF-16 code units.Test
Added 11 new tests in
common/src/util/__tests__/string.test.tscovering:maxLengthbounds testing intruncateStringWithMessageforEND,START, andMIDDLEmodes.Scope
common/only,common/src/util/string.tsand its test file.