Skip to content

Commit

Permalink
refactor String.replaceAll() to Matcher.replaceAll() #596
Browse files Browse the repository at this point in the history
  • Loading branch information
ikarsokolov committed Feb 28, 2016
1 parent 5c77b3d commit 79d4be0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/takes/facets/fork/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.takes.facets.fork;

import java.util.Locale;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString;

Expand All @@ -41,6 +42,11 @@
@EqualsAndHashCode(of = { "high", "low" })
final class MediaType implements Comparable<MediaType> {

/**
* Media type priority clean pattern.
*/
private static final Pattern CLEANER = Pattern.compile("[^0-9\\.]");

/**
* Priority.
*/
Expand Down Expand Up @@ -112,7 +118,7 @@ private static Double priority(final String text) {
final String[] parts = MediaType.split(text);
final Double priority;
if (parts.length > 1) {
final String num = parts[1].replaceAll("[^0-9\\.]", "");
final String num = CLEANER.matcher(parts[1]).replaceAll("");
if (num.isEmpty()) {
priority = 0.0d;
} else {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/takes/misc/Href.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Pattern;

/**
* HTTP URI/HREF.
Expand All @@ -53,6 +54,11 @@
)
public final class Href implements CharSequence {

/**
* Trailer slash pattern.
*/
private static final Pattern TRAILER = Pattern.compile("/$");

/**
* URI (without the query part).
*/
Expand Down Expand Up @@ -195,9 +201,11 @@ public Iterable<String> param(final Object key) {
public Href path(final Object suffix) {
return new Href(
URI.create(
new StringBuilder(this.uri.toString().replaceAll("/$", ""))
.append('/')
.append(Href.encode(suffix.toString())).toString()
new StringBuilder(
TRAILER.matcher(this.uri.toString()).replaceAll("")
)
.append('/')
.append(Href.encode(suffix.toString())).toString()
),
this.params
);
Expand Down

0 comments on commit 79d4be0

Please sign in to comment.