Fix failing RFC822.Mailbox test.
Reverting commit 1d8c4aea broke the mailbox class a bit. This fixes the
issue, introduces Ascii.last_index_of which is needed by Mailbox anyway,
and adds some test for it. Also 'optimises' Ascii.index_of bit as well.
This commit is contained in:
parent
40d9b77999
commit
ba861b8ed1
6 changed files with 79 additions and 15 deletions
|
|
@ -127,7 +127,7 @@ public class Geary.RFC822.MailboxAddress :
|
|||
this.source_route = null;
|
||||
this.address = address;
|
||||
|
||||
int atsign = Ascii.index_of(address, '@');
|
||||
int atsign = Ascii.last_index_of(address, '@');
|
||||
if (atsign > 0) {
|
||||
this.mailbox = address[0:atsign];
|
||||
this.domain = address[atsign + 1:address.length];
|
||||
|
|
@ -136,7 +136,7 @@ public class Geary.RFC822.MailboxAddress :
|
|||
this.domain = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MailboxAddress.imap(string? name, string? source_route, string mailbox, string domain) {
|
||||
this.name = (name != null) ? decode_name(name) : null;
|
||||
this.source_route = source_route;
|
||||
|
|
@ -173,12 +173,12 @@ public class Geary.RFC822.MailboxAddress :
|
|||
}
|
||||
|
||||
string address = mailbox.get_addr();
|
||||
int atsign = address.last_index_of_char('@');
|
||||
int atsign = Ascii.last_index_of(address, '@');
|
||||
if (atsign == -1) {
|
||||
// No @ detected, try decoding in case a mailer (wrongly)
|
||||
// encoded the whole thing and re-try
|
||||
address = decode_address_part(address);
|
||||
atsign = address.last_index_of_char('@');
|
||||
atsign = Ascii.last_index_of(address, '@');
|
||||
}
|
||||
|
||||
if (atsign >= 0) {
|
||||
|
|
|
|||
|
|
@ -12,19 +12,36 @@ extern string g_ascii_strdown(string str, ssize_t len = -1);
|
|||
namespace Geary.Ascii {
|
||||
|
||||
public int index_of(string str, char ch) {
|
||||
// Use a pointer and explicit null check, since testing against
|
||||
// the length of the string as in a traditional for loop will mean
|
||||
// a call to strlen(), making the loop O(n^2)
|
||||
int ret = -1;
|
||||
char *strptr = str;
|
||||
int index = 0;
|
||||
for (;;) {
|
||||
char strch = *strptr++;
|
||||
|
||||
if (strch == String.EOS)
|
||||
return -1;
|
||||
|
||||
if (strch == ch)
|
||||
return index;
|
||||
|
||||
index++;
|
||||
int i = 0;
|
||||
while (*strptr != String.EOS) {
|
||||
if (*strptr++ == ch) {
|
||||
ret = i;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int last_index_of(string str, char ch) {
|
||||
// Use a pointer and explicit null check, since testing against
|
||||
// the length of the string as in a traditional for loop will mean
|
||||
// a call to strlen(), making the loop O(n^2)
|
||||
int ret = -1;
|
||||
char *strptr = str;
|
||||
int i = 0;
|
||||
while (*strptr != String.EOS) {
|
||||
if (*strptr++ == ch) {
|
||||
ret = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool get_next_char(string str, ref int index, out char ch) {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ set(TEST_ENGINE_SRC
|
|||
engine/rfc822-message-test.vala
|
||||
engine/rfc822-message-data-test.vala
|
||||
engine/rfc822-utils-test.vala
|
||||
engine/util-ascii-test.vala
|
||||
engine/util-html-test.vala
|
||||
engine/util-idle-manager-test.vala
|
||||
engine/util-inet-test.vala
|
||||
|
|
|
|||
44
test/engine/util-ascii-test.vala
Normal file
44
test/engine/util-ascii-test.vala
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2018 Michael Gratton <mike@vee.net>
|
||||
*
|
||||
* This software is licensed under the GNU Lesser General Public License
|
||||
* (version 2.1 or later). See the COPYING file in this distribution.
|
||||
*/
|
||||
|
||||
class Geary.Ascii.Test : TestCase {
|
||||
|
||||
public Test() {
|
||||
base("Geary.Ascii.Test");
|
||||
add_test("index_of", index_of);
|
||||
add_test("last_index_of", last_index_of);
|
||||
}
|
||||
|
||||
public void index_of() throws Error {
|
||||
assert_int(-1, Ascii.index_of("", 'a'));
|
||||
assert_int(0, Ascii.index_of("a", 'a'));
|
||||
assert_int(0, Ascii.index_of("aa", 'a'));
|
||||
|
||||
assert_int(0, Ascii.index_of("abcabc", 'a'));
|
||||
assert_int(1, Ascii.index_of("abcabc", 'b'));
|
||||
assert_int(2, Ascii.index_of("abcabc", 'c'));
|
||||
|
||||
assert_int(0, Ascii.index_of("@", '@'));
|
||||
|
||||
assert_int(-1, Ascii.index_of("abc", 'd'));
|
||||
}
|
||||
|
||||
public void last_index_of() throws Error {
|
||||
assert_int(-1, Ascii.last_index_of("", 'a'));
|
||||
assert_int(0, Ascii.last_index_of("a", 'a'));
|
||||
assert_int(1, Ascii.last_index_of("aa", 'a'));
|
||||
|
||||
assert_int(3, Ascii.last_index_of("abcabc", 'a'));
|
||||
assert_int(4, Ascii.last_index_of("abcabc", 'b'));
|
||||
assert_int(5, Ascii.last_index_of("abcabc", 'c'));
|
||||
|
||||
assert_int(0, Ascii.last_index_of("@", '@'));
|
||||
|
||||
assert_int(-1, Ascii.last_index_of("abc", 'd'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -32,6 +32,7 @@ geary_test_engine_sources = [
|
|||
'engine/rfc822-message-test.vala',
|
||||
'engine/rfc822-message-data-test.vala',
|
||||
'engine/rfc822-utils-test.vala',
|
||||
'engine/util-ascii-test.vala',
|
||||
'engine/util-html-test.vala',
|
||||
'engine/util-idle-manager-test.vala',
|
||||
'engine/util-inet-test.vala',
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ int main(string[] args) {
|
|||
engine.add_suite(new Geary.App.ConversationSetTest().get_suite());
|
||||
// Depends on ConversationTest and ConversationSetTest passing
|
||||
engine.add_suite(new Geary.App.ConversationMonitorTest().get_suite());
|
||||
engine.add_suite(new Geary.Ascii.Test().get_suite());
|
||||
engine.add_suite(new Geary.HTML.UtilTest().get_suite());
|
||||
engine.add_suite(new Geary.Imap.DeserializerTest().get_suite());
|
||||
engine.add_suite(new Geary.Imap.CreateCommandTest().get_suite());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue