Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Enhancement issue #3 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ dependencies {
| `range(from:to)` | match text at specified position |
| `ranges(ranges)` | match all texts at specified positions |
| `between(startText:endText)` | match text between two texts |
| `pattern(Pattern...)` | match all occurrences with object Pattern |
| `pattern(String...)` | match all occurrences with string pattern |

#### Step 2: Apply style(s)

Expand Down
19 changes: 18 additions & 1 deletion library/src/main/java/com/jaychang/st/SimpleText.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public SimpleText all(String target) {
return this;
}

public SimpleText pattern(String... patterns) {
Pattern[] listPattern = new Pattern[patterns.length];
for (int patternPosition = 0; patternPosition < patterns.length; patternPosition++) {
listPattern[patternPosition] = Pattern.compile(patterns[patternPosition]);
}
return this.pattern(listPattern);
}

public SimpleText pattern(Pattern... patterns) {
rangeList.clear();
for (Pattern pattern : patterns) {
List<Range> ranges = Utils.ranges(toString(), pattern);
rangeList.addAll(ranges);
}
return this;
}

public SimpleText all() {
rangeList.clear();
Range range = Range.create(0, toString().length());
Expand Down Expand Up @@ -135,7 +152,7 @@ public SimpleText italic() {
}
return this;
}

public SimpleText normal() {
for (Range range : rangeList) {
setSpan(new StyleSpan(Typeface.NORMAL), range.from, range.to, SPAN_MODE);
Expand Down
20 changes: 12 additions & 8 deletions library/src/main/java/com/jaychang/st/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ static List<Integer> indexesOf(String src, String target) {
return positions;
}

static List<Range> ranges(String src, String pattern) {
List<Range> ranges = new ArrayList<>();
Matcher matcher = Pattern.compile(pattern).matcher(src);
while (matcher.find()) {
Range range = Range.create(matcher.start(), matcher.end());
ranges.add(range);
static List<Range> ranges(String src, String pattern) {
return ranges(src, Pattern.compile(pattern));
}

static List<Range> ranges(String src, Pattern pattern) {
List<Range> ranges = new ArrayList<>();
Matcher matcher = pattern.matcher(src);
while (matcher.find()) {
Range range = Range.create(matcher.start(), matcher.end());
ranges.add(range);
}
return ranges;
}
return ranges;
}
}