Skip to content

Instantly share code, notes, and snippets.

@MarkNjunge
Last active February 3, 2019 16:25
Show Gist options
  • Select an option

  • Save MarkNjunge/5f3efb15719cc861be93a7985416e23c to your computer and use it in GitHub Desktop.

Select an option

Save MarkNjunge/5f3efb15719cc861be93a7985416e23c to your computer and use it in GitHub Desktop.

Revisions

  1. MarkNjunge renamed this gist Feb 3, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. MarkNjunge revised this gist Feb 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions DetectSpoilerText.kt
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@ while (text.contains(spoilerTag)) {
    // Get start and end of spoiler tags
    val start = text.indexOf(spoilerTag)
    val end = text.indexOf(spoilerTag, start + spoilerTag.length) - spoilerTag.length

    ranges.add(Pair(start, end))

    // Remove starting spoiler tags
    // This is intentionally done before checking if an end tag exists to prevent an infinite loop
    @@ -19,6 +21,4 @@ while (text.contains(spoilerTag)) {

    // Remove ending spoiler tags
    text = text.replaceRange(end, end + spoilerTag.length, "")

    ranges.add(Pair(start, end))
    }
  3. MarkNjunge created this gist Feb 3, 2019.
    24 changes: 24 additions & 0 deletions DetectSpoilerText.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    var text = editText.text.toString()

    // Save the original text without the tags
    val original = text.replace(spoilerTag, "")

    // Get the character ranges
    val ranges = mutableListOf<Pair<Int, Int>>()
    while (text.contains(spoilerTag)) {
    // Get start and end of spoiler tags
    val start = text.indexOf(spoilerTag)
    val end = text.indexOf(spoilerTag, start + spoilerTag.length) - spoilerTag.length

    // Remove starting spoiler tags
    // This is intentionally done before checking if an end tag exists to prevent an infinite loop
    text = text.replaceRange(start, start + spoilerTag.length, "")

    // If there is no end tag, the text was badly formatted
    if (end <= start) continue

    // Remove ending spoiler tags
    text = text.replaceRange(end, end + spoilerTag.length, "")

    ranges.add(Pair(start, end))
    }