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 getFileInPath(tpath) { 43 return path.parse(tpath).base; 44} 45 46function getPathInPath(tpath) { 47 return path.parse(tpath).dir; 48} 49 50function all(sfrom) { 51 return new RegExp(sfrom, "g"); 52} 53 54function replaceAll(ss, sfrom, sto) { 55 return ss.replace(all(sfrom), sto) 56} 57 58module.exports = { 59 search, 60 match, 61 getFileInPath, 62 getPathInPath, 63 replaceAll, 64 all 65}