Skip to content

Commit

Permalink
Add ignoreCase flag support to span function
Browse files Browse the repository at this point in the history
Fix: #18
  • Loading branch information
neworld committed Oct 10, 2020
1 parent b7cde48 commit f50dd1d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.2.0

- Add `ignoreCase` flag support to `span` function (#18)

### 1.1.0

- Add `lineBackground` and `lineHeight` for Android Q
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ Spans:

Text manipulation:

| Methods | Description |
| --------------- | --------------------------- |
| append(text, spans...) | appends text |
| append(image(...)) | appends image |
| replace(search, replace, spans...)| search and replace text |
| span(search, spans...) | search text and apply spans |
| insert(pos, text, spans...) | insert given text in given position |
| Methods | Description |
| --------------- | --------------------------- |
| append(text, spans...) | appends text |
| append(image(...)) | appends image |
| replace(search, replace, spans...) | search and replace text |
| span(search, ignoreCase = false, spans...)| search text and apply spans |
| insert(pos, text, spans...) | insert given text in given position |

#### How to use
```
Expand Down
28 changes: 25 additions & 3 deletions lib/src/main/java/lt/neworld/spanner/Spanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) {
return this
}

override fun append(text: CharSequence, start: Int, end: Int): Spanner {
override fun append(text: CharSequence?, start: Int, end: Int): Spanner {
super.append(text, start, end)

return this
Expand Down Expand Up @@ -131,19 +131,41 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) {
return this
}

fun span(search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner {
span(0, search, ignoreCase, *spans)

return this
}

fun span(startIndex: Int, search: CharSequence, vararg spans: Span): Spanner {
return span(startIndex, search, false, *spans)
}

fun span(startIndex: Int, search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner {
if (TextUtils.isEmpty(search)) {
setSpans(0, length, *spans)
return this
}

val actualSearch = if (ignoreCase) {
(search.toString() as java.lang.String).toLowerCase()
} else {
search
}

val actualThis = if (ignoreCase) {
(toString() as java.lang.String).toLowerCase()
} else {
this
}

var lastPos: Int = startIndex - 1

while (true) {
lastPos = TextUtils.indexOf(this, search, lastPos + 1)
lastPos = TextUtils.indexOf(actualThis, actualSearch, lastPos + 1)
if (lastPos == -1) break

setSpans(lastPos, lastPos + search.length, *spans)
setSpans(lastPos, lastPos + actualSearch.length, *spans)
}

return this
Expand Down
18 changes: 15 additions & 3 deletions lib/src/test/java/lt/neworld/spanner/SpannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.lang.StringBuilder

/**
* @author Andrius Semionovas
Expand Down Expand Up @@ -110,8 +109,21 @@ class SpannerTest {
assertSpans("foo <StyleSpan>bar</StyleSpan>", Spanner().append("foo bar").span("bar", bold()))
assertSpans("foo", Spanner().append("foo").span("bar", bold()))
assertSpans(
"<StyleSpan>bar</StyleSpan> foo <StyleSpan>bar</StyleSpan>",
Spanner().append("bar foo bar").span("bar", bold())
expected = "<StyleSpan>bar</StyleSpan> foo <StyleSpan>bar</StyleSpan>",
actual = Spanner().append("bar foo bar").span("bar", bold())
)
}

@Test
fun span_text_ignoreCase() {
assertSpans(
expected = "foo <StyleSpan>BAR</StyleSpan>",
actual = Spanner("foo BAR").span("bar", ignoreCase = true, spans = bold())
)
assertSpans("foo", Spanner().append("foo").span("bar", bold()))
assertSpans(
expected = "<StyleSpan>bAR</StyleSpan> foo <StyleSpan>Bar</StyleSpan>",
actual = Spanner().append("bAR foo Bar").span("bar", ignoreCase = true, spans = bold())
)
}

Expand Down

0 comments on commit f50dd1d

Please sign in to comment.