1/* 2* Copyright (c) 2024 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*/ 15 16export function getCurrentTimeString(): string { 17 const now = new Date(); 18 // 使用 padStart 方法确保日期和月份部分是两位数 19 const yearLastTwoDigits = now.getFullYear() % 100; // 获取年份的最后两位 20 const month = (now.getMonth() + 1).toString().padStart(2, '0'); 21 const day = now.getDate().toString().padStart(2, '0'); 22 const hours = now.getHours().toString().padStart(2, '0'); 23 const minutes = now.getMinutes().toString().padStart(2, '0'); 24 const seconds = now.getSeconds().toString().padStart(2, '0'); 25 26 // 拼接成完整的日期时间字符串,年份只显示最后两位 27 const currentTimeString = `${yearLastTwoDigits}${month}${day}${hours}${minutes}${seconds}`; 28 return currentTimeString; 29} 30 31export function replaceAll(s: string, sfrom: string, sto: any) { 32 // Logger.getInstance().debug('[replaceall] s:'+s+' sfrom:'+sfrom+' sto:'+sto); 33 if (s && sfrom && sto || sto == '') { 34 while (s.indexOf(sfrom) >= 0) { 35 s = s.replace(sfrom, sto); 36 } 37 } 38 return s; 39} 40 41export function getTab(tabLv: number) { 42 let tab = ''; 43 for (let i = 0; i < tabLv; ++i) { 44 tab += ' '; 45 } 46 return tab; 47} 48 49export function removeComments(text: string): string { 50 // 移除单行注释,确保不是URL的一部分 51 const singleLineRegex = /(?<!ftp:|https:|http:)\/\/.*$/gm; 52 // 移除多行注释 53 const multiLineRegex = /\/\*[\s\S]*?\*\//gm; 54 55 // 替换单行注释为空 56 let noComments = text.replace(singleLineRegex, ''); 57 // 替换多行注释为空 58 noComments = noComments.replace(multiLineRegex, ''); 59 60 return noComments; 61} 62 63// 随机生成整数 64export function generateRandomInteger(min: number, max: number) { 65 min = Math.ceil(min); 66 max = Math.floor(max); 67 return Math.floor(Math.random() * (max - min + 1)) + min; 68} 69 70// 去除字符串前面的空格 71export function removeTab(str: string) { 72 str = replaceAll(str, '\r\n', ''); 73 str = replaceAll(str, '\r', ''); 74 str = replaceAll(str, '\n', ''); 75 // 去除class中的public: protected: private: 76 str = replaceAll(str, 'public:', ''); 77 str = replaceAll(str, 'protected:', ''); 78 str = replaceAll(str, 'private:', ''); 79 while (str[0] === ' ') { 80 str = str.replace(' ' , '') 81 } 82 return str; 83} 84