100aff185Sopenharmony_ci/* 200aff185Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400aff185Sopenharmony_ci * you may not use this file except in compliance with the License. 500aff185Sopenharmony_ci * You may obtain a copy of the License at 600aff185Sopenharmony_ci * 700aff185Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800aff185Sopenharmony_ci * 900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and 1300aff185Sopenharmony_ci * limitations under the License. 1400aff185Sopenharmony_ci */ 1500aff185Sopenharmony_ciimport { Constants } from '../model/common/Constants'; 1600aff185Sopenharmony_ci 1700aff185Sopenharmony_ciimport { Log } from './Log'; 1800aff185Sopenharmony_ci 1900aff185Sopenharmony_ciconst TAG: string = 'common_StringUtil'; 2000aff185Sopenharmony_ci 2100aff185Sopenharmony_ciexport class StringUtil { 2200aff185Sopenharmony_ci public static formatString(str: string, ...val: any[]): string { 2300aff185Sopenharmony_ci if (str == null) { 2400aff185Sopenharmony_ci Log.error(TAG, 'formatString input is null!') 2500aff185Sopenharmony_ci return ''; 2600aff185Sopenharmony_ci } 2700aff185Sopenharmony_ci let result = str; 2800aff185Sopenharmony_ci for (let index = 0; index < val.length; index++) { 2900aff185Sopenharmony_ci while (result.indexOf(`{${index}}`) != -1) { 3000aff185Sopenharmony_ci result = result.replace(`{${index}}`, val[index]); 3100aff185Sopenharmony_ci } 3200aff185Sopenharmony_ci } 3300aff185Sopenharmony_ci return result; 3400aff185Sopenharmony_ci } 3500aff185Sopenharmony_ci 3600aff185Sopenharmony_ci public static formatStringForNumber(str: string, ...val: any[]): string { 3700aff185Sopenharmony_ci if (str == null) { 3800aff185Sopenharmony_ci Log.error(TAG, 'formatStringForNumber input is null!'); 3900aff185Sopenharmony_ci return ''; 4000aff185Sopenharmony_ci } 4100aff185Sopenharmony_ci let result = str; 4200aff185Sopenharmony_ci for (let index = 0; index < val.length; index++) { 4300aff185Sopenharmony_ci while (result.indexOf('%d') != -1) { 4400aff185Sopenharmony_ci result = result.replace('%d', val[index]); 4500aff185Sopenharmony_ci } 4600aff185Sopenharmony_ci } 4700aff185Sopenharmony_ci return result; 4800aff185Sopenharmony_ci } 4900aff185Sopenharmony_ci 5000aff185Sopenharmony_ci public static connectString(strList: string[], connector: string): string { 5100aff185Sopenharmony_ci if (strList == null || strList.length <= 0 || connector == null) { 5200aff185Sopenharmony_ci Log.error(TAG, 'connectString input is invalid!'); 5300aff185Sopenharmony_ci return ''; 5400aff185Sopenharmony_ci } 5500aff185Sopenharmony_ci let result = ''; 5600aff185Sopenharmony_ci for (let i = 0; i < strList.length; i++) { 5700aff185Sopenharmony_ci if (strList[i] != '') { 5800aff185Sopenharmony_ci result = result + strList[i].trim() + connector; 5900aff185Sopenharmony_ci } 6000aff185Sopenharmony_ci Log.info(TAG, `connectString: ${i} + ${result}`); 6100aff185Sopenharmony_ci } 6200aff185Sopenharmony_ci return result.substr(0, (result.length - connector.length)); 6300aff185Sopenharmony_ci } 6400aff185Sopenharmony_ci 6500aff185Sopenharmony_ci static getIdFromUri(uri: string): number { 6600aff185Sopenharmony_ci let srcIndex = uri.lastIndexOf('/'); 6700aff185Sopenharmony_ci let srcEnd = uri.length; 6800aff185Sopenharmony_ci let srcId = uri.substring(srcIndex + 1, srcEnd); 6900aff185Sopenharmony_ci let fileId = new Number(srcId); 7000aff185Sopenharmony_ci Log.info(TAG, `getIdByUri fileId: ${fileId}`); 7100aff185Sopenharmony_ci return fileId.valueOf(); 7200aff185Sopenharmony_ci } 7300aff185Sopenharmony_ci 7400aff185Sopenharmony_ci /** 7500aff185Sopenharmony_ci * Arraybuffer序列化,String.fromCharCode.apply采用UTF-16编码,需要在Uint16Array视图下进行 7600aff185Sopenharmony_ci * @param buffer 7700aff185Sopenharmony_ci */ 7800aff185Sopenharmony_ci static arraybufferSerialize(buffer: ArrayBuffer): string { 7900aff185Sopenharmony_ci let serializedBuffer: string = String.fromCharCode.apply(null, new Uint16Array(buffer)); 8000aff185Sopenharmony_ci return serializedBuffer; 8100aff185Sopenharmony_ci } 8200aff185Sopenharmony_ci 8300aff185Sopenharmony_ci /** 8400aff185Sopenharmony_ci * 基于UTF-16进行Arraybuffer反序列化,每2个字节为单位进行转换 8500aff185Sopenharmony_ci * @param serializedBuffer 8600aff185Sopenharmony_ci */ 8700aff185Sopenharmony_ci static arraybufferDeserialize(serializedBuffer: string): ArrayBuffer { 8800aff185Sopenharmony_ci let resultBuffer: ArrayBuffer = new ArrayBuffer(serializedBuffer.length * Constants.NUMBER_2); 8900aff185Sopenharmony_ci let bufferView = new Uint16Array(resultBuffer); 9000aff185Sopenharmony_ci for (let i = 0, strLen = serializedBuffer.length; i < strLen; i++) { 9100aff185Sopenharmony_ci bufferView[i] = serializedBuffer.charCodeAt(i); 9200aff185Sopenharmony_ci } 9300aff185Sopenharmony_ci return resultBuffer; 9400aff185Sopenharmony_ci } 9500aff185Sopenharmony_ci 9600aff185Sopenharmony_ci static checkNameInvalid(inputName: string): boolean { 9700aff185Sopenharmony_ci return Constants.NAME_PATTERN.test(inputName); 9800aff185Sopenharmony_ci } 9900aff185Sopenharmony_ci 10000aff185Sopenharmony_ci static isEmpty(value: string): boolean { 10100aff185Sopenharmony_ci return value === undefined || value === '' || value === null; 10200aff185Sopenharmony_ci } 10300aff185Sopenharmony_ci}