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