Skip to content

Instantly share code, notes, and snippets.

@aqzlpm11
Created July 16, 2017 07:56
Show Gist options
  • Save aqzlpm11/9d3848caedb85ef7ee1532b671b3ed8d to your computer and use it in GitHub Desktop.
Save aqzlpm11/9d3848caedb85ef7ee1532b671b3ed8d to your computer and use it in GitHub Desktop.
c++: python_like code in c++. 在C++中以python的方式写部分代码
#ifndef __PYTHON_LIKE__
#define __PYTHON_LIKE__
#include <stdio.h>
#include <string>
#include <fstream>
#include <streambuf>
#include <vector>
#include <iostream>
#include <stdarg.h>
#include <assert.h>
namespace pyl {
// 超级无敌print
template <class T> void print(T v) { std::cout << v << std::endl; }
template <class T,class T2> void print(T v,T2 v2) { std::cout << v << " " << v2 << std::endl; }
template <class T,class T2,class T3> void print(T v,T2 v2,T3 v3) { std::cout << v << " " << v2 << " " << v3 << std::endl; }
template <class T,class T2,class T3,class T4> void print(T v,T2 v2,T3 v3,T4 v4) { std::cout << v << " " << v2 << " " << v3 << " " << v4 << std::endl; }
template <class T,class T2,class T3,class T4,class T5> void print(T v,T2 v2,T3 v3,T4 v4,T5 v5) { std::cout << v << " " << v2 << " " << v3 << " " << v4 << " " << v5 << std::endl; }
template <class T,class T2,class T3,class T4,class T5,class T6> void print(T v,T2 v2,T3 v3,T4 v4,T5 v5,T6 v6) { std::cout << v << " " << v2 << " " << v3 << " " << v4 << " " << v5 << " " << v6 << std::endl; }
template <class T,class T2,class T3,class T4,class T5,class T6,class T7> void print(T v,T2 v2,T3 v3,T4 v4,T5 v5,T6 v6,T7 v7) { std::cout << v << " " << v2 << " " << v3 << " " << v4 << " " << v5 << " " << v6 << " " << v7 << std::endl; }
/*
* 格式化字符串,并返回为string形式
* strf string_format
*/
std::string strf(const char *format, ...){
static const int BUF_SIZE = 256;
std::string ret;
char buffer[BUF_SIZE];
va_list args;
// use vsnprintf to format
va_start(args, format);
int n = vsnprintf(buffer, BUF_SIZE, format, args);
va_end(args);
if (n >= 0 && n < BUF_SIZE) {
// ok
ret = std::string(buffer);
} else if (n >= BUF_SIZE) {
// buffer not enough. try again.
char *buf = new char[n+1];
// format again
va_start(args, format);
int n2 = vsnprintf(buf, n+1, format, args);
va_end(args);
// they must be equal
assert(n2 == n);
delete buf;
ret = std::string(buf);
}
return ret;
}
/*
* 模仿 Python 的 str object
*/
class str: public std::string {
public:
str(){}
str(const char *s): std::string(s){}
str(const std::string &s): std::string(s){}
str(int v) {
*this = strf("%d", v);
}
std::vector<str> split(char seg) {
std::vector<str> res;
const char *cstr = this->c_str();
std::string s;
int i = 0;
int end = strlen(cstr);
while (true) {
while (i < end && cstr[i] == seg) i++;
while (i < end && cstr[i] != seg) {
s = s + cstr[i];
i++;
}
res.push_back(str(s));
s = "";
if (i == end) {
break;
}
}
return res;
}
//////////////// c
template <class T>
str cformat(T v1) {
return str(strf(this->c_str(), v1));
}
template <class T, class T2>
str cformat(T v1, T2 v2) {
return str(strf(this->c_str(), v1, v2));
}
template <class T, class T2, class T3>
str cformat(T v1, T2 v2, T3 v3) {
return str(strf(this->c_str(), v1, v2, v3));
}
friend std::ostream &operator<< (std::ostream& os, const str& s) {
return os << s.c_str();
}
};
class Path {
public:
Path():_path(".") {}
Path(const str &s):_path(s) {}
str read_text(){
std::ifstream t(_path.c_str());
if (!t.is_open()) {
throw strf("[ERROR] IOError: Can't open file: %s", _path.c_str());
}
std::string s((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
return str(s);
}
protected:
str _path;
};
}
#endif // __PYTHON_LIKE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment