1/* 2 * Copyright (c) 2022-2023 Huawei Device 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 */ 15import { Constants } from '../model/common/Constants'; 16 17import { Log } from './Log'; 18 19const TAG: string = 'common_StringUtil'; 20 21export class StringUtil { 22 public static formatString(str: string, ...val: any[]): string { 23 if (str == null) { 24 Log.error(TAG, 'formatString input is null!') 25 return ''; 26 } 27 let result = str; 28 for (let index = 0; index < val.length; index++) { 29 while (result.indexOf(`{${index}}`) != -1) { 30 result = result.replace(`{${index}}`, val[index]); 31 } 32 } 33 return result; 34 } 35 36 public static formatStringForNumber(str: string, ...val: any[]): string { 37 if (str == null) { 38 Log.error(TAG, 'formatStringForNumber input is null!'); 39 return ''; 40 } 41 let result = str; 42 for (let index = 0; index < val.length; index++) { 43 while (result.indexOf('%d') != -1) { 44 result = result.replace('%d', val[index]); 45 } 46 } 47 return result; 48 } 49 50 public static connectString(strList: string[], connector: string): string { 51 if (strList == null || strList.length <= 0 || connector == null) { 52 Log.error(TAG, 'connectString input is invalid!'); 53 return ''; 54 } 55 let result = ''; 56 for (let i = 0; i < strList.length; i++) { 57 if (strList[i] != '') { 58 result = result + strList[i].trim() + connector; 59 } 60 Log.info(TAG, `connectString: ${i} + ${result}`); 61 } 62 return result.substr(0, (result.length - connector.length)); 63 } 64 65 static getIdFromUri(uri: string): number { 66 let srcIndex = uri.lastIndexOf('/'); 67 let srcEnd = uri.length; 68 let srcId = uri.substring(srcIndex + 1, srcEnd); 69 let fileId = new Number(srcId); 70 Log.info(TAG, `getIdByUri fileId: ${fileId}`); 71 return fileId.valueOf(); 72 } 73 74 /** 75 * Arraybuffer序列化,String.fromCharCode.apply采用UTF-16编码,需要在Uint16Array视图下进行 76 * @param buffer 77 */ 78 static arraybufferSerialize(buffer: ArrayBuffer): string { 79 let serializedBuffer: string = String.fromCharCode.apply(null, new Uint16Array(buffer)); 80 return serializedBuffer; 81 } 82 83 /** 84 * 基于UTF-16进行Arraybuffer反序列化,每2个字节为单位进行转换 85 * @param serializedBuffer 86 */ 87 static arraybufferDeserialize(serializedBuffer: string): ArrayBuffer { 88 let resultBuffer: ArrayBuffer = new ArrayBuffer(serializedBuffer.length * Constants.NUMBER_2); 89 let bufferView = new Uint16Array(resultBuffer); 90 for (let i = 0, strLen = serializedBuffer.length; i < strLen; i++) { 91 bufferView[i] = serializedBuffer.charCodeAt(i); 92 } 93 return resultBuffer; 94 } 95 96 static checkNameInvalid(inputName: string): boolean { 97 return Constants.NAME_PATTERN.test(inputName); 98 } 99 100 static isEmpty(value: string): boolean { 101 return value === undefined || value === '' || value === null; 102 } 103}