Skip to content

Commit 2aaf29d

Browse files
committed
Add two new functions to Utils that remove the traling slash in a string, and remove the scheme from URL string.
1 parent 55037d2 commit 2aaf29d

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ public static String getBlogNameFromAccountMap(Map<String, Object> account) {
3636
* Return the blog home URL setting or the host name if home URL is an empty string.
3737
*/
3838
public static String getHomeURLOrHostNameFromAccountMap(Map<String, Object> account) {
39-
String homeURL = MapUtils.getMapStr(account, "homeURL").replace("http://", "").replace("https://", "").trim();
40-
if (homeURL.endsWith("/")) {
41-
homeURL = homeURL.substring(0, homeURL.length() -1);
42-
}
39+
String homeURL = UrlUtils.removeScheme(MapUtils.getMapStr(account, "homeURL"));
40+
homeURL = StringUtils.removeTrailingSlash(homeURL);
4341

4442
if (homeURL.length() == 0) {
4543
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));

WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ public static String capitalize(final String str) {
180180
return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
181181
}
182182

183+
public static String removeTrailingSlash(final String str) {
184+
if (TextUtils.isEmpty(str) || !str.endsWith("/")) {
185+
return str;
186+
}
187+
188+
return str.substring(0, str.length() -1);
189+
}
190+
183191
/*
184192
* Wrap an image URL in a photon URL
185193
* Check out http://developer.wordpress.com/docs/photon/

WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ public static String normalizeUrl(final String urlString) {
138138
}
139139
}
140140

141+
142+
/**
143+
* returns the passed url without the scheme
144+
*/
145+
public static String removeScheme(final String urlString) {
146+
if (urlString == null) {
147+
return null;
148+
}
149+
150+
int doubleslash = urlString.indexOf("//");
151+
if (doubleslash == -1) {
152+
doubleslash = 0;
153+
} else {
154+
doubleslash += 2;
155+
}
156+
157+
return urlString.substring(doubleslash, urlString.length());
158+
}
159+
141160
/**
142161
* returns the passed url without the query parameters
143162
*/

0 commit comments

Comments
 (0)