Skip to content

Instantly share code, notes, and snippets.

@chenzhg33
Last active June 26, 2019 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenzhg33/5409aa6c0c7b26ffbb77184de51a295f to your computer and use it in GitHub Desktop.
Save chenzhg33/5409aa6c0c7b26ffbb77184de51a295f to your computer and use it in GitHub Desktop.
判断一个字符串是否base64编码
bool isBase64(std::string s) {
int len = s.length();
//长度必须是4的倍数
if (len % 4 != 0) {
return false;
}
//去掉末尾=
while (s[len-1] == '=') len--;
//所有的字符必须是 0-9 a-z A-Z + /
for (int i = 0; i < len; i++) {
if ((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '+' || s[i] == '/') {
continue;
}
return false;
}
return true;
}
#define ISWS(c) ((c)==' '||(c)=='\t'||(c)=='\n')
#define ISPUNCT(c) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",(c))!=NULL)
#define ISUPPER(c) ('A' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'Z')
#define ISLOWER(c) ('a' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'z')
#define ISALPHA(c) (ISUPPER(c) || ISLOWER(c))
#define ISDIGIT(c) ('0' <= ((unsigned) (c)) && ((unsigned) (c)) <= '9')
#define ISXDIGIT(c) (strchr("abcdefABCDEF", c)!=NULL)
#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
#define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127)
#define ISVARBEG(c) (ISALPHA(c) || c == '_')
#define ISVARCHR(c) (ISALNUM(c) || c == '_')
#define SW_LOGINIT(sw_file) \
bool sw_can_log() { \
static time_t last_check_time = 0; \
static bool b_canLog = false; \
time_t cur_time = time(nullptr); \
if (cur_time - last_check_time > 10) { \
if (0 == access(sw_file, F_OK)) { \
b_canLog = true; \
} else { \
b_canLog = false; \
} \
last_check_time = cur_time; \
} \
return b_canLog; \
}
#define SW_LOGERR(fmt, args...) if (sw_can_log()) { LogErr(fmt, ##args); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment