Skip to content

Commit

Permalink
Polishing #1367
Browse files Browse the repository at this point in the history
Prefer exact estimations.

Original pull request: #1400.
  • Loading branch information
mp911de committed Sep 4, 2020
1 parent ec37e79 commit 9bb7560
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/main/java/io/lettuce/core/codec/StringCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,18 @@ public void encode(String str, ByteBuf target) {
*/
int sizeOf(String value, boolean estimate) {

if (estimate) {
return (int) averageBytesPerChar * value.length();
}

if (utf8) {
return ByteBufUtil.utf8MaxBytes(value);
}

if (ascii) {
return value.length();
}


if (estimate) {
return (int) averageBytesPerChar * value.length();
}

return (int) maxBytesPerChar * value.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ void encodeAndDecodeIso88591Buf() {
@Test
void estimateSize() {

assertThat(new StringCodec(LettuceCharsets.UTF8).estimateSize(teststring)).isEqualTo((int) (teststring.length() * 1.1));
assertThat(new StringCodec(LettuceCharsets.UTF8).estimateSize(teststring))
.isEqualTo(ByteBufUtil.utf8MaxBytes(teststring));
assertThat(new StringCodec(LettuceCharsets.ASCII).estimateSize(teststring)).isEqualTo(teststring.length());
assertThat(new StringCodec(StandardCharsets.ISO_8859_1).estimateSize(teststring)).isEqualTo(teststring.length());
}
Expand Down

0 comments on commit 9bb7560

Please sign in to comment.