1%%--------------------------------------------------------------------
2%% Test case that exposed a bug (bogus warning) in dialyzer_dataflow
3%% when refining binaries containing UTF-based segments. Reported by
4%% Patrik Nyblom on 4/3/2009 and fixed by Kostis Sagonas on 31/3/2009.
5%%--------------------------------------------------------------------
6
7-module(bs_utf8).
8
9-export([doit/2]).
10
11doit(N, Bin) when is_integer(N), N > 0 ->
12    count_and_find(Bin, N).
13
14count_and_find(Bin, N) when is_binary(Bin) ->
15    cafu(Bin, N, 0, 0, no_pos).
16
17cafu(<<>>, _N, Count, _ByteCount, SavePos) ->
18    {Count, SavePos};
19cafu(<<_/utf8, Rest/binary>>, 0, Count, ByteCount, _SavePos) ->
20    cafu(Rest, -1, Count+1, 0, ByteCount);
21cafu(<<_/utf8, Rest/binary>>, N, Count, _ByteCount, SavePos) when N < 0 ->
22    cafu(Rest, -1, Count+1, 0, SavePos);
23cafu(<<_/utf8, Rest/binary>> = Whole, N, Count, ByteCount, SavePos) ->
24    Delta = byte_size(Whole) - byte_size(Rest),
25    cafu(Rest, N-1, Count+1, ByteCount+Delta, SavePos);
26cafu(_Other, _N, Count, ByteCount, _SavePos) -> % Non Unicode character at end
27    {Count, ByteCount}.
28