1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "iotjs_def.h"
18 #include "iotjs_string.h"
19 #include "iotjs_util.h"
24 iotjs_string_t iotjs_string_create() {
26 IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
35 iotjs_string_t iotjs_string_create_with_size(const char* data, uint32_t size) {
37 IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
42 IOTJS_ASSERT(data != NULL);
43 _this->data = iotjs_buffer_allocate(size);
44 memcpy(_this->data, data, size);
53 iotjs_string_t iotjs_string_create_with_buffer(char* buffer, uint32_t size) {
55 IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
60 IOTJS_ASSERT(buffer != NULL);
70 void iotjs_string_destroy(iotjs_string_t* str) {
71 IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_string_t, str);
73 if (_this->data != NULL) {
74 iotjs_buffer_release(_this->data);
80 bool iotjs_string_is_empty(const iotjs_string_t* str) {
81 const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
83 return _this->size == 0;
87 void iotjs_string_make_empty(iotjs_string_t* str) {
88 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
90 if (_this->data != NULL) {
91 iotjs_buffer_release(_this->data);
98 void iotjs_string_append(iotjs_string_t* str, const char* data, uint32_t size) {
99 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
101 IOTJS_ASSERT(data != NULL);
107 if (_this->data != NULL) {
108 _this->data = iotjs_buffer_reallocate(_this->data, _this->size + size);
110 IOTJS_ASSERT(_this->size == 0);
111 _this->data = iotjs_buffer_allocate(size);
114 memcpy(_this->data + _this->size, data, size);
119 const char* iotjs_string_data(const iotjs_string_t* str) {
120 const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
121 if (_this->data == NULL) {
129 unsigned iotjs_string_size(const iotjs_string_t* str) {
130 const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);