sio2project / oioioi

GNU General Public License v3.0
160 stars 70 forks source link

Fixed string truncation for TestReport's comment #314

Closed metenn closed 6 months ago

metenn commented 6 months ago

Django's Truncator(text).chars(n) seems to ignore all unicode combining characters, resulting in incorrect string truncations.
For example,

string = 'After\u20e1 trunc, this should be 20 characters...'
temp = Truncator(string).chars(20)
print(len(temp))

this outputs:

21

If we were to remove the unicode symbol, it would correctly output 20. Whether this is truly intended or not eludes me.

When trying to put a string truncated in such manner into a CharField, it fails to pass the MaxLengthValidator check, as it internally uses len() for its check.

Since Python 3, strings respect Unicode by default, so slices seem to work fine for truncation.

MasloMaslane commented 6 months ago

This change was tested on sio2.mimuw and works