Files
freeCodeCamp/curriculum/challenges/chinese/22-rosetta-code/rosetta-code-challenges/tokenize-a-string-with-escaping.md
2024-01-24 19:52:36 +01:00

2.8 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
594faaab4e2a8626833e9c3d 使用转义标记字符串 1 302338 tokenize-a-string-with-escaping

--description--

Write a function or program that can split a string at each non-escaped occurrence of a separator character.

它应该接受三个输入参数:

  • The string
  • The separator character
  • The escape character

它应该输出一个字符串列表。

拆分规则:

  • The fields that were separated by the separators, become the elements of the output list.
  • 即使在开始和结束时,也应保留空字段。

转义规则:

  • "Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.
  • 当转义字符位于没有特殊含义的字符之前时,它仍然算作转义字符(但不会做任何特殊的事情)。
  • 用于转义某些内容的每次出现的转义字符不应成为输出的一部分。

证明您的函数满足以下测试用例:

给定字符串

one^|uno||three^^^^|four^^^|^cuatro|

并使用 | 作为分隔符和 ^ 作为转义字符,你的函数应输出以下数组:

  ['one|uno', '', 'three^^', 'four^|cuatro', '']

--hints--

tokenize 应该是一个函数。

assert(typeof tokenize === 'function');

tokenize 应该返回一个数组。

assert(typeof tokenize('a', 'b', 'c') === 'object');

tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^') 应该返回 ['one|uno', '', 'three^^', 'four^|cuatro', '']

assert.deepEqual(tokenize(testStr1, '|', '^'), res1);

tokenize('a@&bcd&ef&&@@hi', '&', '@') 应该返回 ['a&bcd', 'ef', '', '@hi']

assert.deepEqual(tokenize(testStr2, '&', '@'), res2);

--seed--

--after-user-code--

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--

function tokenize(str, sep, esc) {
  return true;
}

--solutions--

// 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
  );
}