Skip to content

Commit

Permalink
fix(watermarker): fix single watermark insertion and extraction (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schiphorst-ISST authored Dec 2, 2024
1 parent 9e6fb05 commit caa84ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ class TextWatermarker(
file.content = result.toString()

// Check if watermark fits at least one time into the text file with given positioning
if (insertPositions.count() < separatedWatermark.count()) {
if (insertPositions.count() < getMinimumInsertPositions(watermark)) {
return OversizedWatermarkWarning(
separatedWatermark.count(),
getMinimumInsertPositions(watermark),
insertPositions.count(),
).into()
}
Expand Down Expand Up @@ -228,7 +228,9 @@ class TextWatermarker(
sequence {
var lastSeparatorPosition = 0
for (separatorPosition in separatorPositions) {
yield(lastSeparatorPosition to separatorPosition - 1)
if (lastSeparatorPosition != separatorPosition - 1) {
yield(lastSeparatorPosition to separatorPosition - 1)
}
lastSeparatorPosition = separatorPosition + 1
}
}
Expand Down Expand Up @@ -326,7 +328,11 @@ class TextWatermarker(
@JsName("getMinimumInsertPositionsBytes")
fun getMinimumInsertPositions(watermark: List<Byte>): Int {
val separatedWatermark = getSeparatedWatermark(watermark)
return separatedWatermark.count()
return if (separatorStrategy is SeparatorStrategy.StartEndSeparatorChars) {
separatedWatermark.count()
} else {
separatedWatermark.count() + 1
}
}

/** Counts the minimum number of insert positions needed in a text to insert the [watermark] */
Expand Down
8 changes: 4 additions & 4 deletions watermarker/src/commonTest/kotlin/unitTest/WatermarkerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class WatermarkerTest {
fun textAddWatermark_watermarkTooLong_warningAndWatermarkedString() {
// Arrange
val watermark = "Hello, world!".encodeToByteArray().asList()
val expectedMessage = TextWatermarker.OversizedWatermarkWarning(53, 49).into().toString()
val expectedMessage = TextWatermarker.OversizedWatermarkWarning(54, 49).into().toString()
val expected =
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temp" +
"or invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero " +
Expand Down Expand Up @@ -169,18 +169,18 @@ class WatermarkerTest {
}

@Test
fun textGetWatermarks_exactlyOneWatermark_successAndWatermark() {
fun textGetWatermarks_exactlyOneWatermark_warningAndWatermark() {
// Arrange
val expected =
listOf(
Watermark.fromString("a"),
Watermark(listOf(0, 97)),
)

// Act
val result = watermarker.textGetWatermarks("a a a a a a a a a a")

// Assert
assertTrue(result.isSuccess)
assertTrue(result.isWarning)
assertEquals(expected, result.value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TextWatermarkerTestJvm {
// Arrange
val file = openTextFile("src/jvmTest/resources/lorem_ipsum.txt")
val watermark = Watermark.fromString("This is a watermark that does not fit")
val expectedMessage = TextWatermarker.OversizedWatermarkWarning(149, 96).into().toString()
val expectedMessage = TextWatermarker.OversizedWatermarkWarning(150, 96).into().toString()

// Act
val result = textWatermarker.addWatermark(file, watermark)
Expand Down

0 comments on commit caa84ae

Please sign in to comment.