8000 Fixed unclosed string exception by tmaxxdd · Pull Request #20 · SnipMeDev/Highlights · GitHub
[go: up one dir, main page]

Skip to content

Fixed unclosed string exception #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed unclosed string exception
  • Loading branch information
tmaxxdd committed Sep 24, 2023
commit 85d4cde9a9129ee3a498127f43e66caaee2e3eb8
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.7.1]

### Fixed
- unclosed string notation during input

## [0.7.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = "dev.snipme"
version = "0.7.0"
version = "0.7.1"

kotlin {
// Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ internal object StringLocator {

private fun findStrings(code: String): List<PhraseLocation> {
val locations = mutableListOf<PhraseLocation>()
val textIndices = mutableListOf<Int>()

// Find index of each string delimiter like " or ' or """
STRING_DELIMITERS.forEach {
val textIndices = mutableListOf<Int>()
textIndices += code.indicesOf(it)
}

// For given indices find words between
for (i in START_INDEX..textIndices.lastIndex step TWO_ELEMENTS) {
locations.add(PhraseLocation(textIndices[i], textIndices[i + 1] + QUOTE_ENDING_POSITION))
// For given indices find words between
for (i in START_INDEX..textIndices.lastIndex step TWO_ELEMENTS) {
if (textIndices.getOrNull(i + 1) != null) {
locations.add(
PhraseLocation(
textIndices[i],
textIndices[i + 1] + QUOTE_ENDING_POSITION
)
)
}
}
}

return locations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.snipme.highlights.internal.locator

import dev.snipme.highlights.internal.printResults
import dev.snipme.highlights.model.PhraseLocation
import kotlin.test.assertEquals
import kotlin.test.Test
Expand Down Expand Up @@ -68,4 +69,29 @@ internal class StringLocatorTest {
assertEquals(PhraseLocation(20, 23), result[0])
assertEquals(PhraseLocation(8, 11), result[1])
}

@Test
fun `No returns location of unclosed string phrase`() {
val testCode = """
val b = "a
val a = 'a
""".trimIndent()

val result = StringLocator.locate(testCode)

assertEquals(0, result.size)
}

@Test
fun `Returns location only of closed string phrase`() {
val testCode = """
val b = 'a
val a = "a"
""".trimIndent()

val result = StringLocator.locate(testCode)

assertEquals(1, result.size)
assertEquals(PhraseLocation(19, 22), result[0])
}
}
0