Skip to content

Commit 43baccc

Browse files
committed
Un-bug (fix?) utf8 truncation (Issue #3)
1 parent 798b0a5 commit 43baccc

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

evalbot.pl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,14 @@ package Evalbot;
280280
my $format_len = length(encode_utf8(sprintf $format_res, $prefix, ''));
281281
if (length(encode_utf8($response)) + $format_len > $max_output_len){
282282
my $target = $max_output_len - 3 - $format_len;
283-
$response =~ s/^.{0,$target}(?![^\x00-\x7F\xC0-\xFF])\K.*//s;
284-
$response .= "";
285-
# my $cut_res = '';
286-
# while ($response =~ /(\X)/g) {
287-
# my $grapheme_bytes = encode_utf8($1);
288-
# $target -= length($grapheme_bytes);
289-
# last if $target < 0;
290-
# $cut_res .= $grapheme_bytes;
291-
# }
292-
# $response = $cut_res.'…';
283+
my $cut_res = '';
284+
while ($response =~ /(\X)/g) {
285+
my $grapheme_bytes = encode_utf8($1);
286+
$target -= length($grapheme_bytes);
287+
last if $target < 0;
288+
$cut_res .= $1;
289+
}
290+
$response = $cut_res.'';
293291
}
294292
return sprintf $format_res, $prefix, $response;
295293
}

0 commit comments

Comments
 (0)