add custom-implemented String

This commit is contained in:
wangzhe 2020-09-15 17:19:54 +08:00
parent 4abb33f151
commit 770c3b05f3
6 changed files with 466 additions and 5 deletions

View File

@ -26,6 +26,7 @@ list(REMOVE_ITEM KERNEL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../nnacl/opt_op_handler.
set(CCSRC
${CMAKE_CURRENT_SOURCE_DIR}/src/lite_session.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/ms_tensor.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/common/string.cc
${TOP_DIR}/src/common/log_adapter.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../core/gvar/logging_level.cc
${TOP_DIR}/src/runtime/allocator.cc

View File

@ -17,15 +17,14 @@
#ifndef MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_
#define MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_
#include <vector>
#include <string>
#include "internal/include/string.h"
struct MSTensor;
struct Node;
using TensorPtr = MSTensor *;
using TensorPtrVector = std::vector<MSTensor *>;
using Uint32Vector = std::vector<uint32_t>;
using String = std::string;
using StringVector = std::vector<std::string>;
using StringVector = std::vector<String>;
using ShapeVector = std::vector<int>;
using NodePtrVector = std::vector<struct Node *>;
using Int32Vector = std::vector<int>;

View File

@ -0,0 +1,100 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERNAL_SRC_STRING_H_
#define INTERNAL_SRC_STRING_H_
#include <string.h>
#include <stdint.h>
typedef struct String {
public:
String();
String(size_t count, char ch);
String(const char *s, size_t count);
explicit String(const char *s);
String(const String &other);
String(const String &other, size_t pos, size_t count = npos);
~String();
String &operator=(const String &str);
String &operator=(const char *str);
char &at(size_t pos);
const char &at(size_t pos) const;
char &operator[](size_t pos);
const char &operator[](size_t pos) const;
char *data() noexcept;
const char *data() const noexcept;
const char *c_str() const noexcept;
// capacity
bool empty() const noexcept;
size_t size() const noexcept;
size_t length() const noexcept;
// operations
void clear() noexcept;
String &append(size_t count, const char ch);
String &append(const String &str);
String &append(const char *s);
String &operator+=(const String &str);
String &operator+=(const char *str);
String &operator+=(const char ch);
int compare(const String &str) const;
int compare(const char *str) const;
String substr(size_t pos = 0, size_t count = npos) const;
static const size_t npos = -1;
private:
size_t size_;
char *buffer_;
} String;
bool operator==(const String &lhs, const String &rhs);
bool operator==(const String &lhs, const char *rhs);
bool operator==(const char *lhs, const String rhs);
bool operator!=(const String &lhs, const String &rhs);
bool operator!=(const String &lhs, const char *rhs);
bool operator!=(const char *lhs, const String rhs);
bool operator<(const String &lhs, const String &rhs);
bool operator<(const String &lhs, const char *rhs);
bool operator<(const char *lhs, const String rhs);
bool operator>(const String &lhs, const String &rhs);
bool operator>(const String &lhs, const char *rhs);
bool operator>(const char *lhs, const String rhs);
bool operator<=(const String &lhs, const String &rhs);
bool operator<=(const String &lhs, const char *rhs);
bool operator<=(const char *lhs, const String rhs);
bool operator>=(const String &lhs, const String &rhs);
bool operator>=(const String &lhs, const char *rhs);
bool operator>=(const char *lhs, const String rhs);
String to_String(int32_t value);
String to_String(int64_t value);
String to_String(uint32_t value);
String to_String(uint64_t value);
String to_String(float value);
String to_String(double value);
String to_String(long double value);
#endif // INTERNAL_SRC_STRING_H_

View File

@ -0,0 +1,357 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal/include/string.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <float.h>
#include <stdint.h>
#include "internal/src/lite_log.h"
String::String() {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
}
String::String(size_t count, char ch) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (count + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memset(buffer_, ch, count);
buffer_[count] = '\0';
size_ = count;
}
String::String(const char *s, size_t count) {
if (s == nullptr) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
return;
}
size_t size_s = strlen(s);
if (size_s <= count) {
size_ = size_s;
} else {
size_ = count;
}
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (size_ + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
strncpy(buffer_, s, size_);
buffer_[size_] = '\0';
}
String::String(const char *s) {
if (s == nullptr) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
return;
}
size_ = strlen(s);
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (size_ + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(buffer_, s, size_ + 1);
}
String::String(const String &other) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (other.size_ + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
size_ = other.size_;
memcpy(buffer_, other.buffer_, size_ + 1);
}
String::String(const String &other, size_t pos, size_t count) {
if (pos >= other.size_) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
} else {
if (count == npos) {
count = other.size_ - pos;
}
if (pos + count > other.size_) {
size_ = other.size_ - pos;
} else {
size_ = count;
}
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (size_ + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
strncpy(buffer_, other.buffer_ + pos, size_);
buffer_[size_] = '\0';
}
}
String::~String() { free(buffer_); }
String &String::operator=(const String &str) {
if (this == &str) {
return *this;
}
free(buffer_);
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (str.size_ + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
size_ = str.size_;
memcpy(buffer_, str.buffer_, size_ + 1);
return *this;
}
String &String::operator=(const char *str) {
free(buffer_);
if (str == nullptr) {
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
return *this;
}
size_t size_s = strlen(str);
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * (size_s + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
size_ = size_s;
memcpy(buffer_, str, size_ + 1);
return *this;
}
char &String::at(size_t pos) {
if (pos >= size_) {
MS_C_EXCEPTION("pos out of range");
}
return buffer_[pos];
}
const char &String::at(size_t pos) const {
if (pos >= size_) {
MS_C_EXCEPTION("pos out of range");
}
return buffer_[pos];
}
char &String::operator[](size_t pos) {
if (pos >= size_) {
MS_C_EXCEPTION("pos out of range");
}
return this->at(pos);
}
const char &String::operator[](size_t pos) const {
if (pos >= size_) {
MS_C_EXCEPTION("pos out of range");
}
return this->at(pos);
}
char *String::data() noexcept { return buffer_; };
const char *String::data() const noexcept { return buffer_; }
const char *String::c_str() const noexcept { return buffer_; }
// capacity
bool String::empty() const noexcept { return size_ == 0; }
size_t String::size() const noexcept { return size_; }
size_t String::length() const noexcept { return size_; }
// operations
void String::clear() noexcept {
free(buffer_);
buffer_ = reinterpret_cast<char *>(malloc(sizeof(char) * 1));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
buffer_[0] = '\0';
size_ = 0;
}
String &String::operator+=(const String &str) {
size_t new_size = size_ + str.size_;
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (new_size + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);
strncat(tmp, str.buffer_, str.size_);
tmp[new_size] = '\0';
free(buffer_);
buffer_ = tmp;
size_ = new_size;
return *this;
}
String &String::operator+=(const char *str) {
if (str == nullptr) {
return *this;
}
size_t str_size = strlen(str);
size_t new_size = size_ + str_size;
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (new_size + 1)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);
strncat(tmp, str, str_size);
tmp[new_size] = '\0';
free(buffer_);
buffer_ = tmp;
size_ = new_size;
return *this;
}
String &String::operator+=(const char ch) {
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (size_ + 2)));
if (buffer_ == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);
tmp[size_] = ch;
tmp[size_ + 1] = '\0';
free(buffer_);
buffer_ = tmp;
size_ += 1;
return *this;
}
String &String::append(size_t count, const char ch) {
(*this) += ch;
return *this;
}
String &String::append(const String &str) {
(*this) += str;
return *this;
}
String &String::append(const char *str) {
if (str == nullptr) {
return *this;
}
(*this) += str;
return *this;
}
int String::compare(const String &str) const { return strcmp(buffer_, str.buffer_); }
int String::compare(const char *str) const { return strcmp(buffer_, str); }
String String::substr(size_t pos, size_t count) const { return String(*this, pos, count); }
String operator+(const String &lhs, const String &rhs) {
String str(lhs);
str += rhs;
return str;
}
String operator+(const String &lhs, const char *rhs) {
String str(lhs);
str += rhs;
return str;
}
String operator+(const char *lhs, const String &rhs) {
String str(lhs);
return str + rhs;
}
String operator+(const String &lhs, const char rhs) {
String str(lhs);
str += rhs;
return str;
}
String operator+(const char lhs, const String &rhs) {
String str(1, lhs);
str += rhs;
return str;
}
bool operator==(const String &lhs, const String &rhs) { return lhs.compare(rhs) == 0; }
bool operator==(const String &lhs, const char *rhs) { return lhs.compare(rhs) == 0; }
bool operator==(const char *lhs, const String rhs) { return rhs.compare(lhs) == 0; }
bool operator!=(const String &lhs, const String &rhs) { return lhs.compare(rhs) != 0; }
bool operator!=(const String &lhs, const char *rhs) { return lhs.compare(rhs) != 0; }
bool operator!=(const char *lhs, const String rhs) { return rhs.compare(lhs) != 0; }
bool operator<(const String &lhs, const String &rhs) { return lhs.compare(rhs) < 0; }
bool operator<(const String &lhs, const char *rhs) { return lhs.compare(rhs) < 0; }
bool operator<(const char *lhs, const String rhs) { return rhs.compare(lhs) >= 0; }
bool operator>(const String &lhs, const String &rhs) { return lhs.compare(rhs) > 0; }
bool operator>(const String &lhs, const char *rhs) { return lhs.compare(rhs) > 0; }
bool operator>(const char *lhs, const String rhs) { return rhs.compare(lhs) <= 0; }
bool operator<=(const String &lhs, const String &rhs) { return lhs.compare(rhs) <= 0; }
bool operator<=(const String &lhs, const char *rhs) { return lhs.compare(rhs) <= 0; }
bool operator<=(const char *lhs, const String rhs) { return rhs.compare(lhs) > 0; }
bool operator>=(const String &lhs, const String &rhs) { return lhs.compare(rhs) >= 0; }
bool operator>=(const String &lhs, const char *rhs) { return lhs.compare(rhs) >= 0; }
bool operator>=(const char *lhs, const String rhs) { return rhs.compare(lhs) < 0; }
String to_String(int32_t value) {
char tmp[sizeof(int32_t) * 4];
snprintf(tmp, sizeof(int32_t) * 4, "%d", value);
return String(tmp, strlen(tmp));
}
String to_String(int64_t value) {
char tmp[sizeof(int64_t) * 4];
snprintf(tmp, sizeof(int64_t) * 4, "%ld", value);
return String(tmp, strlen(tmp));
}
String to_String(u_int32_t value) {
char tmp[sizeof(u_int32_t) * 4];
snprintf(tmp, sizeof(unsigned) * 4, "%u", value);
return String(tmp, strlen(tmp));
}
String to_String(u_int64_t value) {
char tmp[sizeof(u_int64_t) * 4];
snprintf(tmp, sizeof(u_int64_t) * 4, "%lu", value);
return String(tmp, strlen(tmp));
}
String to_String(float value) {
char tmp[FLT_MAX_10_EXP + 20];
snprintf(tmp, FLT_MAX_10_EXP + 20, "%f", value);
return String(tmp, strlen(tmp));
}
String to_String(double value) {
char tmp[DBL_MAX_10_EXP + 20];
snprintf(tmp, DBL_MAX_10_EXP + 20, "%f", value);
return String(tmp, strlen(tmp));
}
String to_String(long double value) {
char tmp[LDBL_MAX_10_EXP + 20];
snprintf(tmp, DBL_MAX_10_EXP + 20, "%Lf", value);
return String(tmp, strlen(tmp));
}

View File

@ -32,6 +32,9 @@
#define LITE_LOG_ERROR(...) \
printf("[ERROR] [%s %s] [%s] [%d] %s\n", __DATE__, __TIME__, __FILE__, __LINE__, __VA_ARGS__)
#define MS_ASSERT(f) assert(f)
#define MS_C_EXCEPTION(...) \
printf("[EXCEPTION] [%s %s] [%s] [%d] %s\n", __DATE__, __TIME__, __FILE__, __LINE__, __VA_ARGS__); \
exit(1)
#else
#define LITE_DEBUG_LOG(...)
#define LITE_INFO_LOG(...)
@ -39,6 +42,7 @@
#define LITE_ERROR_LOG(...)
#define LITE_LOG_ERROR(...)
#define MS_ASSERT(f) ((void)0)
#define MS_C_EXCEPTION(...) exit(1)
#endif
#endif // MINDSPORE_LITE_INTERNAL_SRC_LITE_LOG_H_

View File

@ -73,13 +73,13 @@ STATUS ReadProtoFromBinaryFile(const char *file, google::protobuf::Message *mess
std::string realPath = RealPath(file);
if (realPath.empty()) {
MS_LOG(ERROR) << "Weight file path " << file << " is not valid";
MS_LOG(ERROR) << "Binary proto file path " << file << " is not valid";
return RET_ERROR;
}
std::ifstream fs(realPath, std::ifstream::in | std::ifstream::binary);
if (!fs.is_open()) {
MS_LOG(ERROR) << "Open weight file " << file << " failed.";
MS_LOG(ERROR) << "Open binary proto file " << file << " failed.";
return RET_ERROR;
}