• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 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*/
15const path = require('path');
16
17function search(ss, data) {
18    ss = replaceAll(ss, '\\.', '\\.');
19    let reg = new RegExp(ss);
20    let tt = reg.exec(data);
21    if (tt === null || tt === undefined) {
22        return null;
23    }
24    let ret = { 'regs': [] };
25    for (let i = 0; i < tt.length; i++) {
26        let p = data.indexOf(tt[i]);
27        if (tt[i] === null || tt[i] === undefined) {
28            ret.regs.push([-1, -1]);
29        }
30        else {
31            ret.regs.push([p, p + tt[i].length]);
32        }
33    }
34
35    return ret;
36}
37
38function match(ss, data) {
39    let tt = search(ss, data);
40    if (tt !== null && tt !== undefined && tt.regs[0][0] === 0) {
41        return tt;
42    }
43    return null;
44}
45
46function removeReg(data, reg) {
47    return data.substring(0, reg[0]) + data.substring(reg[1], data.length);
48}
49
50function getReg(data, reg) {
51    return data.substring(reg[0], reg[1]);
52}
53
54function getFileInPath(tpath) {
55    return path.parse(tpath).base;
56}
57
58function getPathInPath(tpath) {
59    return path.parse(tpath).dir;
60}
61
62function all(sfrom) {
63    return new RegExp(sfrom, 'g');
64}
65
66function replaceAll(ss, sfrom, sto) {
67    return ss.replace(all(sfrom), sto);
68}
69
70function pathJoin(...args) {
71    return path.join(...args);
72}
73
74module.exports = {
75    search,
76    match,
77    removeReg,
78    getReg,
79    getFileInPath,
80    getPathInPath,
81    pathJoin,
82    replaceAll,
83    all,
84};