Created
May 29, 2018 08:12
-
-
Save hsnks100/324338320c89d44e8cf741f30eea0b21 to your computer and use it in GitHub Desktop.
string-util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "string-util.h" | |
| #include <zlib.h> | |
| #include <memory.h> | |
| using namespace std; | |
| string insertSeparator(const string& s, char separator, int width) | |
| { | |
| string ss = s; | |
| string::iterator i = ss.end() - width; | |
| while (i > ss.begin()) | |
| i = ss.insert(i, separator) - width; | |
| return ss; | |
| } | |
| string replaceAll(const string &str, const string &pattern, const string &replace) | |
| { | |
| string result = str; | |
| string::size_type pos = 0; | |
| string::size_type offset = 0; | |
| while((pos = result.find(pattern, offset)) != string::npos) | |
| { | |
| result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace); | |
| offset = pos + replace.size(); | |
| } | |
| return result; | |
| } | |
| /** Compress a STL string using zlib with given compression level and return | |
| * the binary data. */ | |
| std::string compressString(const std::string& str, | |
| int compressionlevel) | |
| { | |
| z_stream zs; // z_stream is zlib's control structure | |
| memset(&zs, 0, sizeof(zs)); | |
| if (deflateInit(&zs, compressionlevel) != Z_OK) | |
| throw(std::runtime_error("deflateInit failed while compressing.")); | |
| zs.next_in = (Bytef*)str.data(); | |
| zs.avail_in = str.size(); // set the z_stream's input | |
| int ret; | |
| char outbuffer[32768]; | |
| std::string outstring; | |
| // retrieve the compressed bytes blockwise | |
| do { | |
| zs.next_out = reinterpret_cast<Bytef*>(outbuffer); | |
| zs.avail_out = sizeof(outbuffer); | |
| ret = deflate(&zs, Z_FINISH); | |
| if (outstring.size() < zs.total_out) { | |
| // append the block to the output string | |
| outstring.append(outbuffer, | |
| zs.total_out - outstring.size()); | |
| } | |
| } while (ret == Z_OK); | |
| deflateEnd(&zs); | |
| if (ret != Z_STREAM_END) { // an error occurred that was not EOF | |
| std::ostringstream oss; | |
| oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; | |
| throw(std::runtime_error(oss.str())); | |
| } | |
| return outstring; | |
| } | |
| /** Decompress an STL string using zlib and return the original data. */ | |
| std::string decompressString(const std::string& str) | |
| { | |
| z_stream zs; // z_stream is zlib's control structure | |
| memset(&zs, 0, sizeof(zs)); | |
| if (inflateInit(&zs) != Z_OK) | |
| throw(std::runtime_error("inflateInit failed while decompressing.")); | |
| zs.next_in = (Bytef*)str.data(); | |
| zs.avail_in = str.size(); | |
| int ret; | |
| char outbuffer[32768]; | |
| std::string outstring; | |
| // get the decompressed bytes blockwise using repeated calls to inflate | |
| do { | |
| zs.next_out = reinterpret_cast<Bytef*>(outbuffer); | |
| zs.avail_out = sizeof(outbuffer); | |
| ret = inflate(&zs, 0); | |
| if (outstring.size() < zs.total_out) { | |
| outstring.append(outbuffer, | |
| zs.total_out - outstring.size()); | |
| } | |
| } while (ret == Z_OK); | |
| inflateEnd(&zs); | |
| if (ret != Z_STREAM_END) { // an error occurred that was not EOF | |
| std::ostringstream oss; | |
| oss << "Exception during zlib decompression: (" << ret << ") " | |
| << zs.msg; | |
| throw(std::runtime_error(oss.str())); | |
| } | |
| return outstring; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <string> | |
| #include <stdarg.h> | |
| #include <algorithm> | |
| #include <iterator> | |
| #include <locale> | |
| #include <functional> | |
| #include <chrono> | |
| #include <sstream> | |
| #include <iostream> | |
| #include <string> | |
| #include <stdexcept> | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <sstream> | |
| #include <zlib.h> | |
| std::string compressString(const std::string& str, | |
| int compressionlevel = Z_BEST_COMPRESSION); | |
| /** Decompress an STL string using zlib and return the original data. */ | |
| std::string decompressString(const std::string& str); | |
| std::string insertSeparator(const std::string& s, char separator = ',', int width = 3); | |
| std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it requires zlib