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 57489fb commit c2b2e1a
Show file tree
Hide file tree
Showing 2 changed files with 7 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 @@ -191,18 +191,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 @@ -116,7 +116,7 @@ void encodeAndDecodeIso88591Buf() {
void estimateSize() {

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

0 comments on commit c2b2e1a

Please sign in to comment.