InstruçÔes
Tokenizar uma string com escape
Escreva uma função ou programa que possa dividir uma string em cada ocorrĂȘncia sem escape de um caractere separador.
Ela deve aceitar trĂȘs parĂąmetros de entrada:
<ul>
<li>The <strong>string</strong></li>
<li>The <strong>separator character</strong></li>
<li>The <strong>escape character</strong></li>
</ul>
Ela deve ter como saĂda uma lista de strings.
Regras para a divisĂŁo:
<ul>
<li>The fields that were separated by the separators, become the elements of the output list.</li>
<li>Empty fields should be preserved, even at the start and end.</li>
</ul>
Regras para o escape:
<ul>
<li>"Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.</li>
<li>When the escape character precedes a character that has no special meaning, it still counts as an escape (but does not do anything special).</li>
<li>Each occurrences of the escape character that was used to escape something, should not become part of the output.</li>
</ul>
Demonstre que sua função satisfaz o seguinte caso de teste:
Dada a string
<pre>one^|uno||three^^^^|four^^^|^cuatro|</pre>
e usando
| como separador e ^ como caractere de escape, a função deve dar como resultado o seguinte array:
<pre> ['one|uno', '', 'three^^', 'four^|cuatro', '']
</pre>
O que fazer:
Testes:
- `tokenize` deve ser uma função.
- `tokenize` deve retornar um array.
- `tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')` deve retornar `['one|uno', '', 'three^^', 'four^|cuatro', '']`
- `tokenize('a@&bcd&ef&&@@hi', '&', '@')` deve retornar `['a&bcd', 'ef', '', '@hi']`
- `tokenize('hello^|world|how^are^you^|', '|', '^')` deve retornar `['hello|world', 'howareyou|']`
Console