i can't believe it's string functions

This commit is contained in:
malloc 2018-02-28 17:07:39 -06:00
parent 3c32001e4f
commit 9b267df5e8
7 changed files with 102 additions and 27 deletions

View file

@ -1,4 +1,3 @@
set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
cmake_minimum_required(VERSION 2.6)
project(server)
@ -7,6 +6,8 @@ file(GLOB_RECURSE server_src
"src/*.cpp"
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static -g")
add_executable(server ${server_src})
install(TARGETS server RUNTIME DESTINATION bin)

View file

@ -1,10 +1,16 @@
#include <iostream>
#include <string>
#include "sock/tcpsock.hpp"
#include "utils/string.hpp"
int main(int argc, char **argv) {
//auto sock = sosc::TcpClient();
std::cout << "compiled on windows" << std::endl;
std::string a = "this!!is!!a!!test";
auto b = sosc::str::split(a, "!!");
std::for_each(b.begin(), b.end(), [](std::string& i) {
std::cout << i << std::endl;
});
return 0;
}

View file

@ -1,2 +0,0 @@
#include "ip.hpp"

View file

@ -1,12 +0,0 @@
#ifndef SOSC_UTIL_IP_H
#define SOSC_UTIL_IP_H
namespace sosc {
class IpAddress {
protected:
};
}
#endif

View file

@ -1,10 +1,63 @@
#ifndef SOSC_UTIL_NET_H
#define SOSC_UTIL_NET_H
namespace sosc {
namespace util {
namespace net {
#include <string>
#include <chrono>
}}}
#undef htons
#undef HTONS
#undef htonus
#undef HTONUS
#undef ntohs
#undef NTOHS
#undef ntohus
#undef NTOHUS
#undef htonl
#undef HTONL
#undef htonul
#undef HTONUL
#undef ntohl
#undef NTOHL
#undef ntohul
#undef NTOHUL
#undef htonll
#undef HTONLL
#undef htonull
#undef HTONULL
#undef ntohll
#undef NTOHLL
#undef ntohull
#undef NTOHULL
#define HTONS (X) sosc::net::htonv<int16_t>(X)
#define HTONUS(X) sosc::net::htonv<uint16_t>(X)
#define NTOHS (X) sosc::net::ntohv<int16_t>(X)
#define NTOHUS(X) sosc::net::ntohv<uint16_t>(X)
#define HTONL (X) sosc::net::htonv<int32_t>(X)
#define HTONUL(X) sosc::net::htonv<uint32_t>(X)
#define NTOHL (X) sosc::net::ntohv<int32_t>(X)
#define NTOHUL(X) sosc::net::ntohv<uint32_t>(X)
#define HTONLL (X) sosc::net::htonv<int64_t>(X)
#define HTONULL(X) sosc::net::htonv<uint64_t>(X)
#define NTOHLL (X) sosc::net::ntohv<int64_t>(X)
#define NTOHULL(X) sosc::net::ntohv<uint64_t>(X)
namespace sosc {
typedef std::chrono::system_clock time;
namespace net {
class IpAddress {
};
template<typename T = uint32_t>
std::string htonv(T host_var);
template<typename T = uint32_t>
T ntohv(std::string net_var, size_t offset);
}}
#endif

View file

@ -14,9 +14,9 @@ std::string sosc::str::ltrim(std::string str) {
std::string* sosc::str::ltrim(std::string* str) {
int marker = 0;
for(; marker < str->length(); ++marker)
if((*str)[marker] < 0x21) break;
if((*str)[marker] > 0x20) break;
str->erase(0, marker - 1);
str->erase(0, marker);
return str;
}
@ -26,10 +26,10 @@ std::string sosc::str::rtrim(std::string str) {
std::string* sosc::str::rtrim(std::string *str) {
int marker = 0;
for(; marker < str->length(); --marker)
if((*str)[str->length() - marker - 1] < 0x21) break;
for(; marker < str->length(); ++marker)
if((*str)[str->length() - marker - 1] > 0x20) break;
str->erase(str->length() - marker - 1, marker);
str->erase(str->length() - marker, marker);
return str;
}
@ -64,7 +64,7 @@ std::vector<std::string> sosc::str::split
while((chunk_end = str.find(delimiter, chunk_end))
!= std::string::npos && count != 0)
{
parts.push_back(str.substr(chunk_start, chunk_start - chunk_end));
parts.push_back(str.substr(chunk_start, chunk_end - chunk_start));
chunk_start = (chunk_end += delimiter.length());
--count;
}
@ -92,3 +92,25 @@ std::string sosc::str::join(const std::vector<std::string>& parts,
return assembled;
}
bool sosc::str::starts
(const std::string& str, const std::string& start)
{
if(start.length() > str.length())
return false;
return std::mismatch(
start.begin(), start.end(), str.begin()
).first == start.end();
}
bool sosc::str::ends
(const std::string& str, const std::string& end)
{
if(end.length() > str.length())
return false;
return std::mismatch(
end.begin(), end.end(), str.end() - end.length()
).first == end.end();
}

View file

@ -6,6 +6,10 @@
#include <sstream>
#include <algorithm>
#undef tostr
#undef TOSTR
#define TOSTR(X) std::to_string(X)
namespace sosc {
namespace str {
std::string trim (std::string str);
@ -26,6 +30,9 @@ std::string join(const std::vector<std::string>& parts,
char delimiter, int count = -1);
std::string join(const std::vector<std::string>& parts,
std::string delimiter, int count = -1);
bool starts(const std::string& str, const std::string& start);
bool ends(const std::string& str, const std::string& end);
}}
#endif