Skip to content

Commit c1acdbe

Browse files
remove method (commons-app#6028)
Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
1 parent 2c8c441 commit c1acdbe

File tree

2 files changed

+3
-75
lines changed

2 files changed

+3
-75
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadMediaDetailAdapter.java

+3-35
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ public void bind(int position) {
302302

303303
removeButton.setOnClickListener(v -> removeDescription(uploadMediaDetail, position));
304304
captionListener = new AbstractTextWatcher(
305-
captionText -> uploadMediaDetail.setCaptionText(convertIdeographicSpaceToLatinSpace(
306-
removeLeadingAndTrailingWhitespace(captionText))));
305+
captionText -> uploadMediaDetail.setCaptionText(
306+
convertIdeographicSpaceToLatinSpace(captionText.strip()))
307+
);
307308
descriptionListener = new AbstractTextWatcher(
308309
descriptionText -> uploadMediaDetail.setDescriptionText(descriptionText));
309310
captionItemEditText.addTextChangedListener(captionListener);
@@ -547,39 +548,6 @@ private void setUpRecentLanguagesSection(final List<Language> recentLanguages) {
547548
}
548549
}
549550

550-
/**
551-
* Removes any leading and trailing whitespace from the source text.
552-
*
553-
* @param source input string
554-
* @return a string without leading and trailing whitespace
555-
*/
556-
public String removeLeadingAndTrailingWhitespace(String source) {
557-
// This method can be replaced with the inbuilt String::strip when updated to JDK 11.
558-
// Note that String::trim does not adequately remove all whitespace chars.
559-
int firstNonWhitespaceIndex = 0;
560-
while (firstNonWhitespaceIndex < source.length()) {
561-
if (Character.isWhitespace(source.charAt(firstNonWhitespaceIndex))) {
562-
firstNonWhitespaceIndex++;
563-
} else {
564-
break;
565-
}
566-
}
567-
if (firstNonWhitespaceIndex == source.length()) {
568-
return "";
569-
}
570-
571-
int lastNonWhitespaceIndex = source.length() - 1;
572-
while (lastNonWhitespaceIndex > firstNonWhitespaceIndex) {
573-
if (Character.isWhitespace(source.charAt(lastNonWhitespaceIndex))) {
574-
lastNonWhitespaceIndex--;
575-
} else {
576-
break;
577-
}
578-
}
579-
580-
return source.substring(firstNonWhitespaceIndex, lastNonWhitespaceIndex + 1);
581-
}
582-
583551
/**
584552
* Convert Ideographic space to Latin space
585553
*

app/src/test/kotlin/fr/free/nrw/commons/upload/UploadMediaDetailAdapterUnitTest.kt

-40
Original file line numberDiff line numberDiff line change
@@ -269,50 +269,10 @@ class UploadMediaDetailAdapterUnitTest {
269269
verify(adapterView, times(3)).adapter
270270
}
271271

272-
@Test
273-
fun testRemoveLeadingAndTrailingWhitespace() {
274-
// empty space
275-
val test1 = " test "
276-
val expected1 = "test"
277-
Assert.assertEquals(expected1, viewHolder.removeLeadingAndTrailingWhitespace(test1))
278-
279-
val test2 = " test test "
280-
val expected2 = "test test"
281-
Assert.assertEquals(expected2, viewHolder.removeLeadingAndTrailingWhitespace(test2))
282-
283-
// No whitespace
284-
val test3 = "No trailing space"
285-
val expected3 = "No trailing space"
286-
Assert.assertEquals(expected3, viewHolder.removeLeadingAndTrailingWhitespace(test3))
287-
288-
// blank string
289-
val test4 = " \r \t "
290-
val expected4 = ""
291-
Assert.assertEquals(expected4, viewHolder.removeLeadingAndTrailingWhitespace(test4))
292-
}
293-
294-
@Test
295-
fun testRemoveLeadingAndTrailingInstanceTab() {
296-
val test = "\ttest\t"
297-
val expected = "test"
298-
Assert.assertEquals(expected, viewHolder.removeLeadingAndTrailingWhitespace(test))
299-
}
300-
301-
@Test
302-
fun testRemoveLeadingAndTrailingCarriageReturn() {
303-
val test = "\rtest\r"
304-
val expected = "test"
305-
Assert.assertEquals(expected, viewHolder.removeLeadingAndTrailingWhitespace(test))
306-
}
307-
308272
@Test
309273
fun testCaptionJapaneseCharacters() {
310274
val test1 = "テスト テスト"
311275
val expected1 = "テスト テスト"
312276
Assert.assertEquals(expected1, viewHolder.convertIdeographicSpaceToLatinSpace(test1))
313-
314-
val test2 = " \r \t テスト \r \t "
315-
val expected2 = "テスト"
316-
Assert.assertEquals(expected2, viewHolder.removeLeadingAndTrailingWhitespace(test2))
317277
}
318278
}

0 commit comments

Comments
 (0)