mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
oatpp::String: better convenience methods.
This commit is contained in:
parent
f4d0502056
commit
6ee5ddba3b
@ -26,6 +26,9 @@
|
||||
|
||||
#include "oatpp/core/data/stream/BufferStream.hpp"
|
||||
#include "oatpp/core/utils/ConversionUtils.hpp"
|
||||
#include "oatpp/core/data/share/MemoryLabel.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace oatpp { namespace data { namespace mapping { namespace type {
|
||||
|
||||
@ -36,7 +39,46 @@ String::String(const std::shared_ptr<std::string>& ptr, const type::Type* const
|
||||
throw std::runtime_error("Value type does not match");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String String::loadFromFile(const char* filename) {
|
||||
std::ifstream file (filename, std::ios::in|std::ios::binary|std::ios::ate);
|
||||
if (file.is_open()) {
|
||||
auto result = data::mapping::type::String(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read((char*) result->data(), result->size());
|
||||
file.close();
|
||||
return result;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void String::saveToFile(const char* filename) const {
|
||||
std::ofstream fs(filename, std::ios::out | std::ios::binary);
|
||||
if(m_ptr != nullptr) {
|
||||
fs.write(m_ptr->data(), m_ptr->size());
|
||||
}
|
||||
fs.close();
|
||||
}
|
||||
|
||||
const std::string& String::operator*() const {
|
||||
return this->m_ptr.operator*();
|
||||
}
|
||||
|
||||
bool String::equalsCI(const std::string& other) {
|
||||
auto ciLabel = share::StringKeyLabelCI(m_ptr);
|
||||
return ciLabel == other;
|
||||
}
|
||||
|
||||
bool String::equalsCI(const String& other) {
|
||||
auto ciLabel = share::StringKeyLabelCI(m_ptr);
|
||||
return ciLabel == other;
|
||||
}
|
||||
|
||||
bool String::equalsCI(const char* other) {
|
||||
auto ciLabel = share::StringKeyLabelCI(m_ptr);
|
||||
return ciLabel == other;
|
||||
}
|
||||
|
||||
String operator + (const char* a, const String& b) {
|
||||
data::stream::BufferOutputStream stream;
|
||||
stream << a << b;
|
||||
|
@ -123,14 +123,26 @@ public:
|
||||
: type::ObjectWrapper<std::string, __class::String>(std::forward<String>(other))
|
||||
{}
|
||||
|
||||
const std::string& operator*() const {
|
||||
if (this->m_ptr == nullptr) throw std::runtime_error("[oatpp::data::mapping::type::String] Error: m_ptr points to null.");
|
||||
return this->m_ptr.operator*();
|
||||
}
|
||||
/**
|
||||
* Load data from file and store in &id:oatpp::String;.
|
||||
* @param filename - name of the file.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static String loadFromFile(const char* filename);
|
||||
|
||||
operator std::string() const
|
||||
{
|
||||
if (this->m_ptr == nullptr) throw std::runtime_error("[oatpp::data::mapping::type::String] Error: m_ptr points to null.");
|
||||
/**
|
||||
* Save content of the buffer to file.
|
||||
* @param filename - name of the file.
|
||||
*/
|
||||
void saveToFile(const char* filename) const;
|
||||
|
||||
const std::string& operator*() const;
|
||||
|
||||
operator std::string() const {
|
||||
if (this->m_ptr == nullptr) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::String::operator std::string() const]: "
|
||||
"Error. Null pointer.");
|
||||
}
|
||||
return this->m_ptr.operator*();
|
||||
}
|
||||
|
||||
@ -176,41 +188,26 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Case insensitive compare.
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
bool equalsCI(const std::string& other);
|
||||
|
||||
inline bool equalsCI(const std::string &sb) {
|
||||
if (this->m_ptr == nullptr ) return false;
|
||||
const std::string& sa = this->m_ptr.operator*();
|
||||
return (sa.size() == sb.size()) && std::equal(sa.begin(), sa.end(), sb.begin(),
|
||||
[] (char a, char b) -> bool {
|
||||
return std::tolower(a) == std::tolower(b);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Case insensitive compare.
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
bool equalsCI(const String& other);
|
||||
|
||||
inline bool equalsCI(const String &b) {
|
||||
if (this->m_ptr == nullptr && b.m_ptr == nullptr) return true;
|
||||
if (this->m_ptr == nullptr && b.m_ptr != nullptr) return false;
|
||||
if (this->m_ptr != nullptr && b.m_ptr == nullptr) return false;
|
||||
const std::string& sb = *b;
|
||||
return this->equalsCI(sb);
|
||||
}
|
||||
|
||||
inline bool equalsCI(const char *b) {
|
||||
if (this->m_ptr == nullptr && b == nullptr) return true;
|
||||
if (this->m_ptr == nullptr && b != nullptr) return false;
|
||||
if (this->m_ptr != nullptr && b == nullptr) return false;
|
||||
|
||||
size_t lb = strlen(b);
|
||||
const std::string& sa = this->m_ptr.operator*();
|
||||
|
||||
if (sa.size() != lb) return false;
|
||||
const char *ba = b;
|
||||
|
||||
for ( const auto &ca: sa ) {
|
||||
if ( std::tolower(ca) != std::tolower(*ba)) return false;
|
||||
ba++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Case insensitive compare.
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
bool equalsCI(const char* str);
|
||||
|
||||
template<typename T,
|
||||
typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
|
||||
|
@ -25,6 +25,8 @@
|
||||
#ifndef oatpp_data_share_MemoryLabel_hpp
|
||||
#define oatpp_data_share_MemoryLabel_hpp
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "oatpp/core/data/mapping/type/Primitive.hpp"
|
||||
#include "oatpp/core/utils/String.hpp"
|
||||
|
||||
@ -64,9 +66,15 @@ public:
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param str
|
||||
* @param ptr
|
||||
*/
|
||||
MemoryLabel(const std::shared_ptr<std::string>& str) : MemoryLabel(str, str->data(), (v_buff_size) str->size()) {}
|
||||
MemoryLabel(const std::shared_ptr<std::string>& ptr) :
|
||||
MemoryLabel(
|
||||
ptr,
|
||||
ptr ? ptr->data() : nullptr,
|
||||
ptr ? (v_buff_size) ptr->size() : 0
|
||||
)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -105,7 +113,7 @@ public:
|
||||
*/
|
||||
void captureToOwnMemory() const {
|
||||
if(!m_memoryHandle || m_memoryHandle->data() != (const char*)m_data || m_memoryHandle->size() != m_size) {
|
||||
m_memoryHandle.reset(new std::string((const char*) m_data, m_size));
|
||||
m_memoryHandle = std::make_shared<std::string>((const char*) m_data, m_size);
|
||||
m_data = (p_char8) m_memoryHandle->data();
|
||||
}
|
||||
}
|
||||
@ -117,7 +125,8 @@ public:
|
||||
* @return - `true` if equals.
|
||||
*/
|
||||
bool equals(const char* data) const {
|
||||
return utils::String::compare(m_data, m_size, data, std::strlen(data)) == 0;
|
||||
auto len = data != nullptr ? std::strlen(data) : 0;
|
||||
return utils::String::compare(m_data, m_size, data, len) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -170,7 +179,13 @@ public:
|
||||
StringKeyLabel() : MemoryLabel() {};
|
||||
|
||||
StringKeyLabel(std::nullptr_t) : MemoryLabel() {}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param ptr
|
||||
*/
|
||||
StringKeyLabel(const std::shared_ptr<std::string>& ptr) : MemoryLabel(ptr) {}
|
||||
|
||||
StringKeyLabel(const std::shared_ptr<std::string>& memoryHandle, const char* data, v_buff_size size);
|
||||
StringKeyLabel(const char* constText);
|
||||
StringKeyLabel(const String& str);
|
||||
@ -229,6 +244,8 @@ public:
|
||||
|
||||
StringKeyLabelCI(std::nullptr_t) : MemoryLabel() {}
|
||||
|
||||
StringKeyLabelCI(const std::shared_ptr<std::string>& ptr) : MemoryLabel(ptr) {}
|
||||
|
||||
StringKeyLabelCI(const std::shared_ptr<std::string>& memoryHandle, const char* data, v_buff_size size);
|
||||
StringKeyLabelCI(const char* constText);
|
||||
StringKeyLabelCI(const String& str);
|
||||
@ -242,7 +259,8 @@ public:
|
||||
}
|
||||
|
||||
inline bool operator==(const char* str) const {
|
||||
return utils::String::compareCI(m_data, m_size, str, std::strlen(str)) == 0;
|
||||
auto len = str != nullptr ? std::strlen(str) : 0;
|
||||
return utils::String::compareCI(m_data, m_size, str, len) == 0;
|
||||
}
|
||||
|
||||
inline bool operator!=(const char* str) const {
|
||||
@ -289,7 +307,7 @@ namespace std {
|
||||
|
||||
result_type operator()(oatpp::data::share::StringKeyLabel const& s) const noexcept {
|
||||
|
||||
p_char8 data = (p_char8) s.getData();
|
||||
auto data = (p_char8) s.getData();
|
||||
result_type result = 0;
|
||||
for(v_buff_size i = 0; i < s.getSize(); i++) {
|
||||
v_char8 c = data[i];
|
||||
@ -309,7 +327,7 @@ namespace std {
|
||||
|
||||
result_type operator()(oatpp::data::share::StringKeyLabelCI const& s) const noexcept {
|
||||
|
||||
p_char8 data = (p_char8) s.getData();
|
||||
auto data = (p_char8) s.getData();
|
||||
result_type result = 0;
|
||||
for(v_buff_size i = 0; i < s.getSize(); i++) {
|
||||
v_char8 c = data[i] | 32;
|
||||
|
@ -24,28 +24,8 @@
|
||||
|
||||
#include "String.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace oatpp { namespace utils {
|
||||
|
||||
data::mapping::type::String String::loadFromFile(const char* filename) {
|
||||
std::ifstream file (filename, std::ios::in|std::ios::binary|std::ios::ate);
|
||||
if (file.is_open()) {
|
||||
auto result = data::mapping::type::String(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read((char*) result->data(), result->size());
|
||||
file.close();
|
||||
return result;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void String::saveToFile(const data::mapping::type::String& data, const char* filename) {
|
||||
std::ofstream fs(filename, std::ios::out | std::ios::binary);
|
||||
fs.write(data->data(), data->size());
|
||||
fs.close();
|
||||
}
|
||||
|
||||
v_buff_size String::compare(const void* data1, v_buff_size size1, const void* data2, v_buff_size size2) {
|
||||
|
||||
if(data1 == data2) return 0;
|
||||
|
@ -25,7 +25,6 @@
|
||||
#ifndef oatpp_utils_String_hpp
|
||||
#define oatpp_utils_String_hpp
|
||||
|
||||
#include "oatpp/core/data/mapping/type/Primitive.hpp"
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
|
||||
namespace oatpp { namespace utils {
|
||||
@ -36,19 +35,6 @@ namespace oatpp { namespace utils {
|
||||
class String {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Load data from file and store in &id:oatpp::String;.
|
||||
* @param filename - name of the file.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static data::mapping::type::String loadFromFile(const char* filename);
|
||||
|
||||
/**
|
||||
* Save content of the buffer to file.
|
||||
* @param filename - name of the file.
|
||||
*/
|
||||
static void saveToFile(const data::mapping::type::String& data, const char* filename);
|
||||
|
||||
/**
|
||||
* Compare data1, data2 using `std::memcmp`.
|
||||
* *It's safe to pass nullptr for data1/data2*
|
||||
|
@ -72,7 +72,7 @@ void SchemaMigration::migrate() {
|
||||
break;
|
||||
|
||||
case SOURCE_FILE:
|
||||
script = utils::String::loadFromFile(source.param->c_str());
|
||||
script = oatpp::String::loadFromFile(source.param->c_str());
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -221,6 +221,99 @@ void StringTest::onRun() {
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "test compareCI methods 1");
|
||||
|
||||
oatpp::String s1 = "hello";
|
||||
|
||||
{
|
||||
oatpp::String s2;
|
||||
OATPP_ASSERT(!s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
const char* s2 = nullptr;
|
||||
OATPP_ASSERT(!s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "test compareCI methods 2");
|
||||
|
||||
oatpp::String s1;
|
||||
|
||||
{
|
||||
oatpp::String s2 = "hello";
|
||||
OATPP_ASSERT(!s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
std::string s2 = "hello";
|
||||
OATPP_ASSERT(!s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
const char* s2 = "hello";
|
||||
OATPP_ASSERT(!s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
oatpp::String s2;
|
||||
OATPP_ASSERT(s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
const char* s2 = nullptr;
|
||||
OATPP_ASSERT(s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_ASSERT(s1.equalsCI(nullptr));
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
bool exceptionThrown = false;
|
||||
|
||||
try {
|
||||
std::string s2 = s1;
|
||||
} catch (const std::runtime_error& e) {
|
||||
exceptionThrown = true;
|
||||
}
|
||||
|
||||
OATPP_ASSERT(exceptionThrown);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_LOGI(TAG, "test compareCI methods 3");
|
||||
|
||||
oatpp::String s1 = "hello";
|
||||
|
||||
{
|
||||
oatpp::String s2 = "HELLO";
|
||||
OATPP_ASSERT(s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
std::string s2 = "HELLO";
|
||||
OATPP_ASSERT(s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
const char* s2 = "HELLO";
|
||||
OATPP_ASSERT(s1.equalsCI(s2));
|
||||
}
|
||||
|
||||
{
|
||||
OATPP_ASSERT(s1.equalsCI("HELLO"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}}}}}}
|
||||
|
Loading…
Reference in New Issue
Block a user