mirror of
https://github.com/moparisthebest/k-9
synced 2025-01-30 23:00:09 -05:00
Change ImapUtility to use 'long' for the values of sequence sets
This commit is contained in:
parent
a37c95b456
commit
2ad748fad7
@ -54,11 +54,8 @@ public class ImapUtility {
|
|||||||
for (String item : setItems) {
|
for (String item : setItems) {
|
||||||
if (item.indexOf(':') == -1) {
|
if (item.indexOf(':') == -1) {
|
||||||
// simple item
|
// simple item
|
||||||
try {
|
if (isNumberValid(item)) {
|
||||||
Integer.parseInt(item); // Don't need the value; just ensure it's valid
|
|
||||||
list.add(item);
|
list.add(item);
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
Log.d(K9.LOG_TAG, "Invalid UID value", e);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// range
|
// range
|
||||||
@ -91,23 +88,46 @@ public class ImapUtility {
|
|||||||
if (range != null) {
|
if (range != null) {
|
||||||
int colonPos = range.indexOf(':');
|
int colonPos = range.indexOf(':');
|
||||||
if (colonPos > 0) {
|
if (colonPos > 0) {
|
||||||
int first = Integer.parseInt(range.substring(0, colonPos));
|
long first = Long.parseLong(range.substring(0, colonPos));
|
||||||
int second = Integer.parseInt(range.substring(colonPos + 1));
|
long second = Long.parseLong(range.substring(colonPos + 1));
|
||||||
if (first < second) {
|
if (is32bitValue(first) && is32bitValue(second)) {
|
||||||
for (int i = first; i <= second; i++) {
|
if (first < second) {
|
||||||
list.add(Integer.toString(i));
|
for (long i = first; i <= second; i++) {
|
||||||
|
list.add(Long.toString(i));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (long i = first; i >= second; i--) {
|
||||||
|
list.add(Long.toString(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = first; i >= second; i--) {
|
Log.d(K9.LOG_TAG, "Invalid range: " + range);
|
||||||
list.add(Integer.toString(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Log.d(K9.LOG_TAG, "Invalid range value", e);
|
Log.d(K9.LOG_TAG, "Invalid range value: " + range, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isNumberValid(String number) {
|
||||||
|
try {
|
||||||
|
long value = Long.parseLong(number);
|
||||||
|
if (is32bitValue(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(K9.LOG_TAG, "Invalid UID value: " + number);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean is32bitValue(long value) {
|
||||||
|
return ((value & ~0xFFFFFFFFL) == 0L);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,14 @@ public class ImapUtilityTest extends TestCase {
|
|||||||
actual = ImapUtility.getImapSequenceValues("1");
|
actual = ImapUtility.getImapSequenceValues("1");
|
||||||
MoreAsserts.assertEquals(expected, actual.toArray());
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
|
expected = new String[] {"2147483648"}; // Integer.MAX_VALUE + 1
|
||||||
|
actual = ImapUtility.getImapSequenceValues("2147483648");
|
||||||
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
|
expected = new String[] {"4294967295"}; // 2^32 - 1
|
||||||
|
actual = ImapUtility.getImapSequenceValues("4294967295");
|
||||||
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
expected = new String[] {"1", "3", "2"};
|
expected = new String[] {"1", "3", "2"};
|
||||||
actual = ImapUtility.getImapSequenceValues("1,3,2");
|
actual = ImapUtility.getImapSequenceValues("1,3,2");
|
||||||
MoreAsserts.assertEquals(expected, actual.toArray());
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
@ -50,6 +58,11 @@ public class ImapUtilityTest extends TestCase {
|
|||||||
actual = ImapUtility.getImapSequenceValues("1,2:4,9:7");
|
actual = ImapUtility.getImapSequenceValues("1,2:4,9:7");
|
||||||
MoreAsserts.assertEquals(expected, actual.toArray());
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
|
// Test numbers larger than Integer.MAX_VALUE (2147483647)
|
||||||
|
expected = new String[] {"2147483646", "2147483647", "2147483648"};
|
||||||
|
actual = ImapUtility.getImapSequenceValues("2147483646:2147483648");
|
||||||
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
// Test partially invalid sets
|
// Test partially invalid sets
|
||||||
expected = new String[] { "1", "5" };
|
expected = new String[] { "1", "5" };
|
||||||
actual = ImapUtility.getImapSequenceValues("1,x,5");
|
actual = ImapUtility.getImapSequenceValues("1,x,5");
|
||||||
@ -75,6 +88,15 @@ public class ImapUtilityTest extends TestCase {
|
|||||||
expected = new String[0];
|
expected = new String[0];
|
||||||
actual = ImapUtility.getImapSequenceValues("1:x");
|
actual = ImapUtility.getImapSequenceValues("1:x");
|
||||||
MoreAsserts.assertEquals(expected, actual.toArray());
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
|
// Test values larger than 2^32 - 1
|
||||||
|
expected = new String[0];
|
||||||
|
actual = ImapUtility.getImapSequenceValues("4294967296:4294967297");
|
||||||
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
|
|
||||||
|
expected = new String[0];
|
||||||
|
actual = ImapUtility.getImapSequenceValues("4294967296"); // 2^32
|
||||||
|
MoreAsserts.assertEquals(expected, actual.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user