11b8d9b87Sopenharmony_ci/* 21b8d9b87Sopenharmony_ci* Copyright (C) 2021 Huawei Device Co., Ltd. 31b8d9b87Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 41b8d9b87Sopenharmony_ci* you may not use this file except in compliance with the License. 51b8d9b87Sopenharmony_ci* You may obtain a copy of the License at 61b8d9b87Sopenharmony_ci* 71b8d9b87Sopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 81b8d9b87Sopenharmony_ci* 91b8d9b87Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software 101b8d9b87Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 111b8d9b87Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121b8d9b87Sopenharmony_ci* See the License for the specific language governing permissions and 131b8d9b87Sopenharmony_ci* limitations under the License. 141b8d9b87Sopenharmony_ci*/ 151b8d9b87Sopenharmony_ci 161b8d9b87Sopenharmony_ci#ifndef HC_STRING_H 171b8d9b87Sopenharmony_ci#define HC_STRING_H 181b8d9b87Sopenharmony_ci 191b8d9b87Sopenharmony_ci#include "hcf_parcel.h" 201b8d9b87Sopenharmony_ci 211b8d9b87Sopenharmony_ci#ifdef __cplusplus 221b8d9b87Sopenharmony_ciextern "C" { 231b8d9b87Sopenharmony_ci#endif 241b8d9b87Sopenharmony_ci 251b8d9b87Sopenharmony_ci typedef struct HcString { 261b8d9b87Sopenharmony_ci HcParcel parcel; // parcel data, used to storage the string data 271b8d9b87Sopenharmony_ci } HcString; 281b8d9b87Sopenharmony_ci 291b8d9b87Sopenharmony_ci /* 301b8d9b87Sopenharmony_ci * Append string pointer 311b8d9b87Sopenharmony_ci * Notice: It will add '\0' automatically. 321b8d9b87Sopenharmony_ci * @param self: self pointer. 331b8d9b87Sopenharmony_ci * @param str: string pointer. 341b8d9b87Sopenharmony_ci * @return true (ok), false (error) 351b8d9b87Sopenharmony_ci */ 361b8d9b87Sopenharmony_ci bool StringAppendPointer(HcString *self, const char *str); 371b8d9b87Sopenharmony_ci 381b8d9b87Sopenharmony_ci /* 391b8d9b87Sopenharmony_ci * Assign a value to the HcString 401b8d9b87Sopenharmony_ci * Notice: It will add '\0' automatically. 411b8d9b87Sopenharmony_ci * @param self: self pointer. 421b8d9b87Sopenharmony_ci * @param str: assign value of string pointer. 431b8d9b87Sopenharmony_ci * @return true (ok), false (error) 441b8d9b87Sopenharmony_ci */ 451b8d9b87Sopenharmony_ci bool StringSetPointer(HcString *self, const char *str); 461b8d9b87Sopenharmony_ci 471b8d9b87Sopenharmony_ci /* 481b8d9b87Sopenharmony_ci * Assign a value to the HcString with fixed length 491b8d9b87Sopenharmony_ci * Notice: It will add '\0' automatically. 501b8d9b87Sopenharmony_ci * @param self: self pointer. 511b8d9b87Sopenharmony_ci * @param str: assign value of string pointer. 521b8d9b87Sopenharmony_ci * @param len: the length of string. 531b8d9b87Sopenharmony_ci * @return true (ok), false (error) 541b8d9b87Sopenharmony_ci */ 551b8d9b87Sopenharmony_ci bool StringSetPointerWithLength(HcString* self, const char *str, uint32_t len); 561b8d9b87Sopenharmony_ci 571b8d9b87Sopenharmony_ci /* 581b8d9b87Sopenharmony_ci * Get the string pointer data 591b8d9b87Sopenharmony_ci * @param self: self pointer. 601b8d9b87Sopenharmony_ci * @return the pointer data of the string 611b8d9b87Sopenharmony_ci */ 621b8d9b87Sopenharmony_ci const char* StringGet(const HcString *self); 631b8d9b87Sopenharmony_ci 641b8d9b87Sopenharmony_ci /* 651b8d9b87Sopenharmony_ci * Get the length of the string 661b8d9b87Sopenharmony_ci * @param self: self pointer. 671b8d9b87Sopenharmony_ci * @return the length of the string 681b8d9b87Sopenharmony_ci */ 691b8d9b87Sopenharmony_ci uint32_t StringLength(const HcString *self); 701b8d9b87Sopenharmony_ci 711b8d9b87Sopenharmony_ci /* 721b8d9b87Sopenharmony_ci * Find a char from string 731b8d9b87Sopenharmony_ci * @param self: self pointer. 741b8d9b87Sopenharmony_ci * @param c: the char you want find 751b8d9b87Sopenharmony_ci * @param begin: the position find from 761b8d9b87Sopenharmony_ci * @return the position of the char 771b8d9b87Sopenharmony_ci */ 781b8d9b87Sopenharmony_ci int StringFind(const HcString *self, char c, uint32_t begin); 791b8d9b87Sopenharmony_ci 801b8d9b87Sopenharmony_ci /* 811b8d9b87Sopenharmony_ci * Get sub string from a string. 821b8d9b87Sopenharmony_ci * @param self: self pointer. 831b8d9b87Sopenharmony_ci * @param begin: the begin position of the sub string. 841b8d9b87Sopenharmony_ci * @param len: the length of the sub string. 851b8d9b87Sopenharmony_ci * @param dst: the string pointer which saved the sub string content. 861b8d9b87Sopenharmony_ci * @return the operation result. 871b8d9b87Sopenharmony_ci */ 881b8d9b87Sopenharmony_ci bool StringSubString(const HcString *self, uint32_t begin, uint32_t len, HcString* dst); 891b8d9b87Sopenharmony_ci 901b8d9b87Sopenharmony_ci /* 911b8d9b87Sopenharmony_ci * Compare the string with another string. 921b8d9b87Sopenharmony_ci * @param self: self pointer. 931b8d9b87Sopenharmony_ci * @param dst: the pointer of another string. 941b8d9b87Sopenharmony_ci * @return the compare result. 951b8d9b87Sopenharmony_ci * -1: self is smaller than dst 961b8d9b87Sopenharmony_ci * 0: self is equal with dst 971b8d9b87Sopenharmony_ci * 1: self is bigger than dst 981b8d9b87Sopenharmony_ci */ 991b8d9b87Sopenharmony_ci int StringCompare(const HcString *self, const char* dst); 1001b8d9b87Sopenharmony_ci 1011b8d9b87Sopenharmony_ci /* 1021b8d9b87Sopenharmony_ci * Create a string. 1031b8d9b87Sopenharmony_ci * Notice: You should delete string when you don't need the string anymore. 1041b8d9b87Sopenharmony_ci * @return the created string. 1051b8d9b87Sopenharmony_ci */ 1061b8d9b87Sopenharmony_ci HcString CreateString(void); 1071b8d9b87Sopenharmony_ci 1081b8d9b87Sopenharmony_ci /* 1091b8d9b87Sopenharmony_ci * Delete a string. In fact it will not destroy the string, 1101b8d9b87Sopenharmony_ci * but only free the allocated memory of the string and reset the member's value 1111b8d9b87Sopenharmony_ci * of the string. You can continue to use the string if you want. 1121b8d9b87Sopenharmony_ci * Notice: You should delete the string when you don't need it any more to avoid memory leak. 1131b8d9b87Sopenharmony_ci * @param str: The string you want to delete. 1141b8d9b87Sopenharmony_ci */ 1151b8d9b87Sopenharmony_ci void DeleteString(HcString *str); 1161b8d9b87Sopenharmony_ci 1171b8d9b87Sopenharmony_ci#ifdef __cplusplus 1181b8d9b87Sopenharmony_ci} 1191b8d9b87Sopenharmony_ci#endif 1201b8d9b87Sopenharmony_ci#endif 121