1/*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function search(ss, data) {
17  ss = replaceAll(ss, '\\.', '\\.');
18  let reg = new RegExp(ss);
19  let tt = reg.exec(data);
20  if (tt === null) {
21    return null;
22  }
23  let ret = { regs: [] };
24  for (let i = 0; i < tt.length; i++) {
25    let p = data.indexOf(tt[i]);
26    let regs = 'regs';
27    if (tt[i] === null) {
28      ret[regs].push([-1, -1]);
29    } else {
30      ret[regs].push([p, p + tt[i].length]);
31    }
32  }
33
34  return ret;
35}
36
37function match(ss, data) {
38  let tt = search(ss, data);
39  if (tt !== null && tt.regs[0][0] === 0) {
40    return tt;
41  }
42  return null;
43}
44
45function removeReg(data, reg) {
46  return data.substring(0, reg[0]) + data.substring(reg[1], data.length);
47}
48
49function getReg(data, reg) {
50  return data.substring(reg[0], reg[1]);
51}
52
53function all(sfrom) {
54  return new RegExp(sfrom, 'g');
55}
56
57function replaceAll(ss, sfrom, sto) {
58  return ss.replace(all(sfrom), sto);
59}
60
61module.exports = {
62  search,
63  match,
64  removeReg,
65  getReg,
66  replaceAll,
67  all,
68};
69