diff --git a/src/eimap_command.erl b/src/eimap_command.erl --- a/src/eimap_command.erl +++ b/src/eimap_command.erl @@ -55,7 +55,7 @@ %% the line was continued, but there is no more lines ... so this line is now our last partial line. more must be on its way io:format("Missing lines!~p~n~n", [Acc]), BytesAsBinary = integer_to_binary(ContinuationBytes), - process_lines(ParseTaggedLine, Tag, <>, [], Acc, Module); + process_lines(ParseTaggedLine, Tag, <>, [], Acc, Module); process_line(_ContinuationBytes, ParseTaggedLine, IsTagged, Tag, LastPartialLine, Line, [NextLine|MoreLines], Acc, Module) -> { StrippedNextLine, NextContinuationBytes } = eimap_utils:num_literal_continuation_bytes(NextLine), io:format("Connected up the next line: ~p ~i~n", [StrippedNextLine, NextContinuationBytes]), diff --git a/src/eimap_utils.erl b/src/eimap_utils.erl --- a/src/eimap_utils.erl +++ b/src/eimap_utils.erl @@ -102,15 +102,15 @@ num_literal_continuation_bytes(Buffer) when size(Buffer) < 4 -> { Buffer, 0 }; num_literal_continuation_bytes(Buffer) -> - BufferSize = size(Buffer), - case binary:part(Buffer, BufferSize - 2, 2) =:= <<"+}">> of - true -> number_of_bytes_in_continuation(Buffer, BufferSize); + case binary:last(Buffer) =:= $} of + true -> number_of_bytes_in_continuation(Buffer); false -> { Buffer, 0 } end. -number_of_bytes_in_continuation(Buffer, BufferSize) -> - OpenBracePos = find_continuation_open_brace(Buffer, BufferSize - 4), - confirm_continuation(Buffer, BufferSize, OpenBracePos). +number_of_bytes_in_continuation(Buffer) -> + BufferSize = size(Buffer), + OpenBracePos = find_continuation_open_brace(Buffer, BufferSize - 3), + confirm_continuation(Buffer, OpenBracePos). find_continuation_open_brace(_Buffer, 0) -> -1; find_continuation_open_brace(Buffer, Pos) -> @@ -119,10 +119,11 @@ _ -> find_continuation_open_brace(Buffer, Pos - 1) end. -confirm_continuation(Buffer, _BufferSize, -1) -> +confirm_continuation(Buffer, -1) -> { Buffer, 0 }; -confirm_continuation(Buffer, BufferSize, OpenBracePos) -> - try binary_to_integer(binary:part(Buffer, OpenBracePos + 1, BufferSize - OpenBracePos - 3)) of +confirm_continuation(Buffer, OpenBracePos) -> + BufferSize = size(Buffer), + try binary_to_integer(binary:part(Buffer, OpenBracePos + 1, BufferSize - OpenBracePos - 2)) of Result -> { binary:part(Buffer, 0, OpenBracePos), Result } catch _:_ -> { Buffer, 0 } diff --git a/test/eimap_command_tests.erl b/test/eimap_command_tests.erl --- a/test/eimap_command_tests.erl +++ b/test/eimap_command_tests.erl @@ -31,24 +31,24 @@ % input, output % { Binary Response, Binary Tag, Parsed Results } { - <<"* STATUS 1 (MESSAGES 231 {14+}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation now\r\n">>, + <<"* STATUS 1 (MESSAGES 231 {14}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation now\r\n">>, <<"abcd">>, { fini, [ { marked, <<"* STATUS 1 (MESSAGES 231 UIDNEXT 44292)">> } ] } }, { - <<"* STATUS 1a (MESSAGES 231 {14+}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation">>, + <<"* STATUS 1a (MESSAGES 231 {14}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation">>, <<"abcd">>, { more, { <<"abcd OK Begin TLS negotiation">>, [ { marked, <<"* STATUS 1a (MESSAGES 231 UIDNEXT 44292)">> } ], ?MODULE } } }, { - <<"* STATUS 2 (MESSAGES 231 {14+}\r\n">>, + <<"* STATUS 2 (MESSAGES 231 {14}\r\n">>, <<"abcd">>, - { more, { <<"* STATUS 2 (MESSAGES 231 {14+}">>, [], ?MODULE } } + { more, { <<"* STATUS 2 (MESSAGES 231 {14}">>, [], ?MODULE } } }, { - <<"* STATUS 3 (MESSAGES 231 {h14+}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation now\r\n">>, + <<"* STATUS 3 (MESSAGES 231 {h14}\r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation now\r\n">>, <<"abcd">>, - { fini, [ { marked, <<"* STATUS 3 (MESSAGES 231 {h14+}">> }, { marked, <<"UIDNEXT 44292)">> } ] } + { fini, [ { marked, <<"* STATUS 3 (MESSAGES 231 {h14}">> }, { marked, <<"UIDNEXT 44292)">> } ] } }, { <<"* STATUS 4 (MESSAGES 231 \r\nUIDNEXT 44292)\r\nabcd OK Begin TLS negotiation now\r\n">>, diff --git a/test/eimap_utils_tests.erl b/test/eimap_utils_tests.erl --- a/test/eimap_utils_tests.erl +++ b/test/eimap_utils_tests.erl @@ -183,16 +183,15 @@ Data = [ { <<"abcd">>, { <<"abcd">>, 0 } }, - { <<"abcd{5+}">>, { <<"abcd">>, 5 } }, - { <<"abcd{100+}">>, { <<"abcd">>, 100 } }, - { <<"123abcd{100+}">>, { <<"123abcd">>, 100 } }, - { <<"ab{123abcd{100+}">>, { <<"ab{123abcd">>, 100 } }, - { <<"ab{123abcd{1{00+}">>, { <<"ab{123abcd{1">>, 0 } }, - { <<"abcd{aa0+}">>, { <<"abcd{aa0+}">>, 0 } }, - { <<"abcd{10aa0+}">>, { <<"abcd{10aa0+}">>, 0 } }, - { <<"abcd100+}">>, { <<"abcd100+}">>, 0 } }, - { <<"abcd{100}">>, { <<"abcd{100}">>, 0 } }, - { <<"abcd100+}">>, { <<"abcd100+}">>, 0 } } + { <<"abcd{5}">>, { <<"abcd">>, 5 } }, + { <<"abcd{100}">>, { <<"abcd">>, 100 } }, + { <<"123abcd{100}">>, { <<"123abcd">>, 100 } }, + { <<"ab{123abcd{100}">>, { <<"ab{123abcd">>, 100 } }, + { <<"ab{123abcd{1{00}">>, { <<"ab{123abcd{1">>, 0 } }, + { <<"abcd{aa0}">>, { <<"abcd{aa0}">>, 0 } }, + { <<"abcd{10aa0}">>, { <<"abcd{10aa0}">>, 0 } }, + { <<"abcd100}">>, { <<"abcd100}">>, 0 } }, + { <<"abcd100}">>, { <<"abcd100}">>, 0 } } ], lists:foldl(fun({ Input, Output}, Acc) -> [?_assertEqual(Output, eimap_utils:num_literal_continuation_bytes(Input)) | Acc] end, [], Data).