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 re = require("./re"); 16let vscode = null; 17try { 18 vscode = require('vscode'); 19} 20catch (err) { 21 vscode = null; 22} 23 24const NUM_CONST_MAP = new Map([ 25 [0, "XNapiTool::ZERO"], [1, "XNapiTool::ONE"], [2, "XNapiTool::TWO"], [3, "XNapiTool::THREE"], 26 [4, "XNapiTool::FOUE"], [5, "XNapiTool::FIVE"], [6, "XNapiTool::SIX"], [7, "XNapiTool::SEVEN"], 27 [8, "XNapiTool::EIGHT"], [9, "XNapiTool::NINE"] 28]); 29 30function print(...args) { 31 if (vscode) { 32 vscode.window.showInformationMessage(...args); 33 } 34 console.log(...args); 35} 36 37String.prototype.format = function (...args) { 38 var result = this; 39 let reg = new RegExp("%[sd]{1}") 40 for (let i = 0; i < args.length; i++) { 41 let p = result.search(reg) 42 if (p < 0) break; 43 result = result.substring(0, p) + args[i] + result.substring(p + 2, result.length) 44 } 45 return result; 46} 47 48String.prototype.replaceAll = function (...args) { 49 let result = this; 50 while (result.indexOf(args[0]) >= 0) { 51 result = result.replace(args[0], args[1]) 52 } 53 return result; 54} 55 56function checkOutBody(body, off, flag, binside) { 57 off = off || 0; 58 flag = flag || ["{", "}"]; 59 binside = binside || false; 60 let idx = { 61 "(": ")", 62 "{": "}", 63 "<": ">", 64 //"<": "<", 65 //">": ">", 66 }; 67 let csl = {}; 68 let csr = {}; 69 for (let f in idx) { 70 csl[f] = 0 71 csr[idx[f]] = 0 72 } 73 let cs1 = 0 74 if (flag[0].length > 0 && body.substring(off, off + flag[0].length) != flag[0]) { 75 return null; 76 } 77 78 for (let i = off + flag[0].length; i < body.length; i++) { 79 if (body[i] == '"') cs1 += 1 80 if (cs1 % 2 == 0) { 81 let tb1 = true; 82 for (let k in csl) { 83 if (csl[k] != csr[idx[k]]) { 84 tb1 = false; 85 break; 86 } 87 } 88 if (tb1 && body.substring(i, i + flag[1].length) == flag[1]) { 89 if (binside) 90 return body.substring(off + flag[0].length, i); 91 return body.substring(off, i + flag[1].length); 92 } 93 94 if (body[i] in csl) { 95 csl[body[i]] += 1; 96 if (body[i] in csr) csr[body[i]] += 1; 97 } 98 if (body[i] in csr) { 99 if (!(body[i] == '>' && body[i-1] == '=')) { // 尖括号匹配时忽略关键字 "=>" 100 csr[body[i]] += 1; 101 } 102 } 103 } 104 } 105 106 return null; 107} 108 109function removeExplains(data) { 110 while (data.indexOf("/*") >= 0) { 111 let i1 = data.indexOf("/*") 112 let i2 = data.indexOf("*/") + 2 113 data = data.substring(0, i1) + data.substring(i2, data.length) 114 } 115 while (true) { 116 let tt = re.search("\n *//([a-zA-Z .]+)\n", data) 117 if (tt != null) { 118 data = data.substring(0, tt.regs[0][0]) + data.substring(tt.regs[0][1], data.length) 119 } 120 else break; 121 } 122 return data 123} 124 125function getLicense(data) { 126 while (data.indexOf("/*") >= 0) { 127 let i1 = data.indexOf("/*") 128 let i2 = data.indexOf("*/") + 2 129 let licenseData = data.substring(i1, i2) 130 if (licenseData.search("Copyright")) { 131 return licenseData 132 } 133 } 134} 135 136function removeEmptyLine(data) { 137 while (data.indexOf("\r") >= 0) { 138 data = data.replace("\r", "") 139 } 140 while (data.indexOf(" \n") >= 0) { 141 data = data.replace(" \n", "\n") 142 } 143 while (data.indexOf("\n ") >= 0) { 144 data = data.replace("\n ", "\n") 145 } 146 while (data.indexOf("\n\n") >= 0) { 147 data = data.replace("\n\n", "\n") 148 } 149 while (data.indexOf("\n") == 0) { 150 data = data.substring(1, data.length) 151 } 152 while (data.indexOf(" ") == 0) { 153 data = data.substring(1, data.length) 154 } 155 return data 156} 157 158function replaceTab(data) { 159 while (data.indexOf("\t") >= 0) { 160 data = data.replace("\t", " ") 161 } 162 return data 163} 164 165function removeEmptyLine2(data) { 166 while (data.indexOf(" \n")) 167 data = data.replace(" \n", "\n") 168 while (data.indexOf("\n\n\n")) 169 data = data.replace("\n\n\n", "\n\n") 170 return data 171} 172 173function replaceAll(s, sfrom, sto) { 174 while (s.indexOf(sfrom) >= 0) { 175 s = s.replace(sfrom, sto) 176 } 177 return s; 178} 179 180/** 181 * 比较两个方法是否完全相同 182 * @param func1 方法1 183 * @param func2 方法2 184 * @returns 方法名称与形参是否完全相同 185 */ 186 function isSameFunc(func1, func2) { 187 if (func1.name != func2.name) { // 判断方法名称是否相同 188 return false; 189 } 190 191 let func1ParamCount = func1.value.length 192 if (func1ParamCount != func2.value.length) { // 判断方法形参个数是否一样 193 return false; 194 } 195 196 for (let i in func1.value) { // 判断方法每个形参数据类型是否相同 197 if (func1.value[i].type != func2.value[i].type) { 198 if (!(func1.value[i].type.indexOf("NUMBER_TYPE_") >= 0 && 199 func2.value[i].type.indexOf("NUMBER_TYPE_") >= 0)) { 200 return false; 201 } 202 } 203 } 204 205 // 以上全部相同,判定为相同方法 206 return true; 207} 208 209/** 210 * 将方法对象插入列表(重复的方法对象不插入) 211 * @param obj 待插入的方法对象 212 * @param list 目标列表 213 * @returns 是否成功插入列表 214 */ 215 function addUniqFunc2List(obj, list) { 216 for (let i in list) { 217 if (isSameFunc(obj, list[i])) { 218 return false 219 } 220 } 221 list.push(obj) 222 return true 223} 224 225/** 226 * 找到子类中重写父类的方法并将它设置为override 227 * @param parentFunc 父类被重写的方法名 228 * @param childFunclist 子类全部方法列表 229 * @returns void 230 */ 231 function setOverrideFunc(parentFunc, childFunclist) { 232 for (let i in childFunclist) { 233 if (isSameFunc(parentFunc, childFunclist[i])) { 234 childFunclist[i].isOverride = true 235 return 236 } 237 } 238} 239 240/** 241 * 将对象插入列表(名称重复的属性对象不插入) 242 * @param obj 待插入的对象 243 * @param list 目标列表 244 * @returns void 245 */ 246 function addUniqObj2List(obj, list) { 247 for (let i in list) { 248 if (list[i].name === obj.name) { 249 return 250 } 251 } 252 list.push(obj) 253} 254 255/** 256 * 如果方法所在的类为基类,生成的c++函数定义为虚函数 257 * @param data 方法所在的类信息 258 * @param funcInfo 方法信息 259 * return tabStr 缩进,staticStr 静态函数关键词,virtualStr 虚函数关键词, overrideStr 重写关键词 260 */ 261 function getPrefix(data, funcInfo) { 262 let isStatic = funcInfo.isStatic 263 let tabStr = "" 264 let virtualStr = "" 265 let staticStr = isStatic ? "static " : "" 266 if (data.childList) { 267 tabStr = " " // 类中的方法增加一个缩进 268 virtualStr = (data.childList.length > 0 && !isStatic) ? "virtual " : "" //如果是基类中的非静态方法,定义为虚函数 269 } 270 let overrideStr = funcInfo.isOverride ? " override" : "" // 重写了父类方法,需要加上override关键字,否则触发c++门禁告警 271 return [tabStr, staticStr, virtualStr, overrideStr] 272} 273 274function getConstNum(num) { 275 return NUM_CONST_MAP.get(parseInt(num)); 276} 277 278module.exports = { 279 checkOutBody, 280 removeExplains, 281 removeEmptyLine, 282 removeEmptyLine2, 283 replaceAll, 284 print, 285 getLicense, 286 replaceTab, 287 addUniqObj2List, 288 addUniqFunc2List, 289 getPrefix, 290 getConstNum, 291 setOverrideFunc 292} 293