Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit c631e22

Browse files
committed
Use capturing group; Add comments
1 parent ac8d2fc commit c631e22

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/src/main/kotlin/at/bitfire/ical4android/validation/FixInvalidDayOffsetPreprocessor.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@ object FixInvalidDayOffsetPreprocessor : StreamPreprocessor() {
1414
// Examples:
1515
// TRIGGER:-P2DT
1616
// TRIGGER:-PT2D
17-
// REFRESH-INTERVAL;VALUE=DURATION:PT1D
18-
"(?:^|(^(DURATION|REFRESH-INTERVAL|RELATED-TO|TRIGGER));VALUE=)(DURATION|TRIGGER):-?P((T-?\\d+D)|(-?\\d+DT))\$",
17+
// REFRESH-INTERVAL;VALUE=DURATION:-PT1D
18+
"(?:^|^(?:DURATION|REFRESH-INTERVAL|RELATED-TO|TRIGGER);VALUE=)(?:DURATION|TRIGGER):(-?P((T-?\\d+D)|(-?\\d+DT)))$",
1919
setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)
2020
)
2121

2222
override fun fixString(original: String): String {
23-
var s: String = original
23+
var iCal: String = original
2424

2525
// Find all instances matching the defined expression
26-
val found = regexpForProblem().findAll(s)
26+
val found = regexpForProblem().findAll(iCal)
2727

2828
// ..and repair them
2929
for (match in found) {
30-
val matchStr = match.value
31-
val fixed = matchStr
30+
// Fix the duration string
31+
val faultyDuration = match.groupValues[1] // IE: "-PT1D" (faulty)
32+
val fixedDuration = faultyDuration // IE: "-P1D" (fixed)
3233
.replace("PT", "P")
3334
.replace("DT", "D")
34-
s = s.replace(matchStr, fixed)
35+
36+
// Replace the faulty duration with the fixed one in the captured line
37+
val faultyCapture = match.value // IE: "REFRESH-INTERVAL;VALUE=DURATION:-PT1D" (faulty)
38+
val fixedCapture = faultyCapture // IE: "REFRESH-INTERVAL;VALUE=DURATION:-P1D" (fixed)
39+
.replace(faultyDuration, fixedDuration)
40+
41+
// Replace complete faulty line in the iCal string with the fixed one
42+
iCal = iCal.replace(faultyCapture, fixedCapture)
3543
}
36-
return s
44+
return iCal
3745
}
3846

3947
}

lib/src/main/kotlin/at/bitfire/ical4android/validation/StreamPreprocessor.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ abstract class StreamPreprocessor {
1313

1414
abstract fun regexpForProblem(): Regex?
1515

16+
/**
17+
* Fixes an iCalendar string.
18+
*
19+
* @param original The complete iCalendar string
20+
* @return The complete iCalendar string, but fixed
21+
*/
1622
abstract fun fixString(original: String): String
1723

1824
fun preprocess(reader: Reader): Reader {
@@ -21,7 +27,7 @@ abstract class StreamPreprocessor {
2127
val resetSupported = try {
2228
reader.reset()
2329
true
24-
} catch(e: IOException) {
30+
} catch(_: IOException) {
2531
false
2632
}
2733

0 commit comments

Comments
 (0)