Skip to content

Commit f50dd1d

Browse files
committed
Add ignoreCase flag support to span function
Fix: #18
1 parent b7cde48 commit f50dd1d

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 1.2.0
2+
3+
- Add `ignoreCase` flag support to `span` function (#18)
4+
15
### 1.1.0
26

37
- Add `lineBackground` and `lineHeight` for Android Q

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ Spans:
9494

9595
Text manipulation:
9696

97-
| Methods | Description |
98-
| --------------- | --------------------------- |
99-
| append(text, spans...) | appends text |
100-
| append(image(...)) | appends image |
101-
| replace(search, replace, spans...)| search and replace text |
102-
| span(search, spans...) | search text and apply spans |
103-
| insert(pos, text, spans...) | insert given text in given position |
97+
| Methods | Description |
98+
| --------------- | --------------------------- |
99+
| append(text, spans...) | appends text |
100+
| append(image(...)) | appends image |
101+
| replace(search, replace, spans...) | search and replace text |
102+
| span(search, ignoreCase = false, spans...)| search text and apply spans |
103+
| insert(pos, text, spans...) | insert given text in given position |
104104

105105
#### How to use
106106
```

lib/src/main/java/lt/neworld/spanner/Spanner.kt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) {
1515
return this
1616
}
1717

18-
override fun append(text: CharSequence, start: Int, end: Int): Spanner {
18+
override fun append(text: CharSequence?, start: Int, end: Int): Spanner {
1919
super.append(text, start, end)
2020

2121
return this
@@ -131,19 +131,41 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) {
131131
return this
132132
}
133133

134+
fun span(search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner {
135+
span(0, search, ignoreCase, *spans)
136+
137+
return this
138+
}
139+
134140
fun span(startIndex: Int, search: CharSequence, vararg spans: Span): Spanner {
141+
return span(startIndex, search, false, *spans)
142+
}
143+
144+
fun span(startIndex: Int, search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner {
135145
if (TextUtils.isEmpty(search)) {
136146
setSpans(0, length, *spans)
137147
return this
138148
}
139149

150+
val actualSearch = if (ignoreCase) {
151+
(search.toString() as java.lang.String).toLowerCase()
152+
} else {
153+
search
154+
}
155+
156+
val actualThis = if (ignoreCase) {
157+
(toString() as java.lang.String).toLowerCase()
158+
} else {
159+
this
160+
}
161+
140162
var lastPos: Int = startIndex - 1
141163

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

146-
setSpans(lastPos, lastPos + search.length, *spans)
168+
setSpans(lastPos, lastPos + actualSearch.length, *spans)
147169
}
148170

149171
return this

lib/src/test/java/lt/neworld/spanner/SpannerTest.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.junit.Assert.assertEquals
66
import org.junit.Test
77
import org.junit.runner.RunWith
88
import org.robolectric.RobolectricTestRunner
9-
import java.lang.StringBuilder
109

1110
/**
1211
* @author Andrius Semionovas
@@ -110,8 +109,21 @@ class SpannerTest {
110109
assertSpans("foo <StyleSpan>bar</StyleSpan>", Spanner().append("foo bar").span("bar", bold()))
111110
assertSpans("foo", Spanner().append("foo").span("bar", bold()))
112111
assertSpans(
113-
"<StyleSpan>bar</StyleSpan> foo <StyleSpan>bar</StyleSpan>",
114-
Spanner().append("bar foo bar").span("bar", bold())
112+
expected = "<StyleSpan>bar</StyleSpan> foo <StyleSpan>bar</StyleSpan>",
113+
actual = Spanner().append("bar foo bar").span("bar", bold())
114+
)
115+
}
116+
117+
@Test
118+
fun span_text_ignoreCase() {
119+
assertSpans(
120+
expected = "foo <StyleSpan>BAR</StyleSpan>",
121+
actual = Spanner("foo BAR").span("bar", ignoreCase = true, spans = bold())
122+
)
123+
assertSpans("foo", Spanner().append("foo").span("bar", bold()))
124+
assertSpans(
125+
expected = "<StyleSpan>bAR</StyleSpan> foo <StyleSpan>Bar</StyleSpan>",
126+
actual = Spanner().append("bAR foo Bar").span("bar", ignoreCase = true, spans = bold())
115127
)
116128
}
117129

0 commit comments

Comments
 (0)