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) return null; 22 let ret = { "regs": [] } 23 for (let i = 0; i < tt.length; i++) { 24 let p = data.indexOf(tt[i]); 25 if (tt[i] == null) { 26 ret["regs"].push([-1, -1]) 27 } 28 else { 29 ret["regs"].push([p, p + tt[i].length]) 30 } 31 } 32 33 return ret; 34} 35 36function match(ss, data) { 37 let tt = search(ss, data) 38 if (tt != null && tt.regs[0][0] == 0) return tt; 39 return null; 40} 41 42function removeReg(data, reg) { 43 return data.substring(0, reg[0]) + data.substring(reg[1], data.length) 44} 45 46function getReg(data, reg) { 47 return data.substring(reg[0], reg[1]) 48} 49 50function getFileInPath(tpath) { 51 return path.parse(tpath).base; 52} 53 54function getPathInPath(tpath) { 55 return path.parse(tpath).dir; 56} 57 58function all(sfrom) { 59 return new RegExp(sfrom, "g"); 60} 61 62function replaceAll(ss, sfrom, sto) { 63 return ss.replace(all(sfrom), sto) 64} 65 66function pathJoin(...args) { 67 return path.join(...args) 68} 69 70module.exports = { 71 search, 72 match, 73 removeReg, 74 getReg, 75 getFileInPath, 76 getPathInPath, 77 pathJoin, 78 replaceAll, 79 all 80}