remove method (#6028)

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
Parneet Singh 2024-12-14 09:45:52 +05:30 committed by GitHub
parent 2c8c441f25
commit c1acdbe31a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 75 deletions

View file

@ -302,8 +302,9 @@ public class UploadMediaDetailAdapter extends
removeButton.setOnClickListener(v -> removeDescription(uploadMediaDetail, position)); removeButton.setOnClickListener(v -> removeDescription(uploadMediaDetail, position));
captionListener = new AbstractTextWatcher( captionListener = new AbstractTextWatcher(
captionText -> uploadMediaDetail.setCaptionText(convertIdeographicSpaceToLatinSpace( captionText -> uploadMediaDetail.setCaptionText(
removeLeadingAndTrailingWhitespace(captionText)))); convertIdeographicSpaceToLatinSpace(captionText.strip()))
);
descriptionListener = new AbstractTextWatcher( descriptionListener = new AbstractTextWatcher(
descriptionText -> uploadMediaDetail.setDescriptionText(descriptionText)); descriptionText -> uploadMediaDetail.setDescriptionText(descriptionText));
captionItemEditText.addTextChangedListener(captionListener); captionItemEditText.addTextChangedListener(captionListener);
@ -547,39 +548,6 @@ public class UploadMediaDetailAdapter extends
} }
} }
/**
* Removes any leading and trailing whitespace from the source text.
*
* @param source input string
* @return a string without leading and trailing whitespace
*/
public String removeLeadingAndTrailingWhitespace(String source) {
// This method can be replaced with the inbuilt String::strip when updated to JDK 11.
// Note that String::trim does not adequately remove all whitespace chars.
int firstNonWhitespaceIndex = 0;
while (firstNonWhitespaceIndex < source.length()) {
if (Character.isWhitespace(source.charAt(firstNonWhitespaceIndex))) {
firstNonWhitespaceIndex++;
} else {
break;
}
}
if (firstNonWhitespaceIndex == source.length()) {
return "";
}
int lastNonWhitespaceIndex = source.length() - 1;
while (lastNonWhitespaceIndex > firstNonWhitespaceIndex) {
if (Character.isWhitespace(source.charAt(lastNonWhitespaceIndex))) {
lastNonWhitespaceIndex--;
} else {
break;
}
}
return source.substring(firstNonWhitespaceIndex, lastNonWhitespaceIndex + 1);
}
/** /**
* Convert Ideographic space to Latin space * Convert Ideographic space to Latin space
* *

View file

@ -269,50 +269,10 @@ class UploadMediaDetailAdapterUnitTest {
verify(adapterView, times(3)).adapter verify(adapterView, times(3)).adapter
} }
@Test
fun testRemoveLeadingAndTrailingWhitespace() {
// empty space
val test1 = " test "
val expected1 = "test"
Assert.assertEquals(expected1, viewHolder.removeLeadingAndTrailingWhitespace(test1))
val test2 = " test test "
val expected2 = "test test"
Assert.assertEquals(expected2, viewHolder.removeLeadingAndTrailingWhitespace(test2))
// No whitespace
val test3 = "No trailing space"
val expected3 = "No trailing space"
Assert.assertEquals(expected3, viewHolder.removeLeadingAndTrailingWhitespace(test3))
// blank string
val test4 = " \r \t "
val expected4 = ""
Assert.assertEquals(expected4, viewHolder.removeLeadingAndTrailingWhitespace(test4))
}
@Test
fun testRemoveLeadingAndTrailingInstanceTab() {
val test = "\ttest\t"
val expected = "test"
Assert.assertEquals(expected, viewHolder.removeLeadingAndTrailingWhitespace(test))
}
@Test
fun testRemoveLeadingAndTrailingCarriageReturn() {
val test = "\rtest\r"
val expected = "test"
Assert.assertEquals(expected, viewHolder.removeLeadingAndTrailingWhitespace(test))
}
@Test @Test
fun testCaptionJapaneseCharacters() { fun testCaptionJapaneseCharacters() {
val test1 = "テスト テスト" val test1 = "テスト テスト"
val expected1 = "テスト テスト" val expected1 = "テスト テスト"
Assert.assertEquals(expected1, viewHolder.convertIdeographicSpaceToLatinSpace(test1)) Assert.assertEquals(expected1, viewHolder.convertIdeographicSpaceToLatinSpace(test1))
val test2 = " \r \t テスト \r \t "
val expected2 = "テスト"
Assert.assertEquals(expected2, viewHolder.removeLeadingAndTrailingWhitespace(test2))
} }
} }