2021-06-15 00:49:18 -07:00
---
id: 594faaab4e2a8626833e9c3d
2022-02-19 12:56:08 +05:30
title: Tokenizzare una stringa con escaping
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302338
dashedName: tokenize-a-string-with-escaping
---
# --description--
2022-02-19 12:56:08 +05:30
Scrivi una funzione o un programma che può dividere una stringa ad ogni occorrenza di un carattere separatore (salvo escape).
2021-06-15 00:49:18 -07:00
2022-02-19 12:56:08 +05:30
Dovrebbe accettare tre parametri di input:
2021-06-15 00:49:18 -07:00
< ul >
2022-02-19 12:56:08 +05:30
< li > La < strong > stringa< / strong > < / li >
< li > Il carattere < strong > separatore< / strong > < / li >
< li > Il < strong > carattere di escape< / strong > < / li >
2021-06-15 00:49:18 -07:00
< / ul >
2022-02-19 12:56:08 +05:30
Dovrebbe produrre un elenco di stringhe.
2021-06-15 00:49:18 -07:00
2022-02-19 12:56:08 +05:30
Regole di suddivisione:
2021-06-15 00:49:18 -07:00
< ul >
2022-02-19 12:56:08 +05:30
< li > I campi separati dai separatori diventano gli elementi della lista di output.< / li >
< li > I campi vuoti dovrebbero essere preservati, anche all'inizio e alla fine.< / li >
2021-06-15 00:49:18 -07:00
< / ul >
2022-02-19 12:56:08 +05:30
Regole di escape:
2021-06-15 00:49:18 -07:00
< ul >
2022-02-19 12:56:08 +05:30
< li > "Fare l'escape" significa far precedere il carattere da una occorrenza del carattere di escape.< / li >
< li > Quando il carattere di escape precede un carattere che non ha alcun significato speciale, conta ancora come un escape (ma non fa nulla di speciale).< / li >
< li > Ogni occorrenza del carattere di escape che è stato usato per evitare qualcosa, non dovrebbe diventare parte dell'output.< / li >
2021-06-15 00:49:18 -07:00
< / ul >
2022-02-19 12:56:08 +05:30
Dimostra che la tua funzione soddisfa il seguente caso di test:
2021-06-15 00:49:18 -07:00
2022-02-19 12:56:08 +05:30
Data la stringa
2021-06-15 00:49:18 -07:00
< pre > one^|uno||three^^^^|four^^^|^cuatro|< / pre >
2022-02-19 12:56:08 +05:30
e usando `|` come separatore e `^` come carattere di escape la funzione dovrebbe produrre il seguente array:
2021-06-15 00:49:18 -07:00
< pre > ['one|uno', '', 'three^^', 'four^|cuatro', '']
< / pre >
# --hints--
2022-02-19 12:56:08 +05:30
`tokenize` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof tokenize === 'function');
```
2022-02-19 12:56:08 +05:30
`tokenize` dovrebbe restituire un array.
2021-06-15 00:49:18 -07:00
```js
assert(typeof tokenize('a', 'b', 'c') === 'object');
```
2022-02-19 12:56:08 +05:30
`tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')` dovrebbe restituire `['one|uno', '', 'three^^', 'four^|cuatro', '']`
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(tokenize(testStr1, '|', '^'), res1);
```
2022-02-19 12:56:08 +05:30
`tokenize('a@&bcd&ef&&@@hi', '&', '@')` dovrebbe restituire `['a&bcd', 'ef', '', '@hi']`
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(tokenize(testStr2, '& ', '@'), res2);
```
# --seed--
## --after-user-code--
```js
const testStr1 = 'one^|uno||three^^^^|four^^^|^cuatro|';
const res1 = ['one|uno', '', 'three^^', 'four^|cuatro', ''];
// TODO add more tests
const testStr2 = 'a@&bcd&ef&&@ @hi ';
const res2 = ['a& bcd', 'ef', '', '@hi '];
```
## --seed-contents--
```js
function tokenize(str, sep, esc) {
return true;
}
```
# --solutions--
```js
// tokenize :: String -> Character -> Character -> [String]
function tokenize(str, charDelim, charEsc) {
const dctParse = str.split('')
.reduce((a, x) => {
const blnEsc = a.esc;
const blnBreak = !blnEsc & & x === charDelim;
const blnEscChar = !blnEsc & & x === charEsc;
return {
esc: blnEscChar,
token: blnBreak ? '' : (
a.token + (blnEscChar ? '' : x)
),
list: a.list.concat(blnBreak ? a.token : [])
};
}, {
esc: false,
token: '',
list: []
});
return dctParse.list.concat(
dctParse.token
);
}
```