本文整理汇总了C++中CStrRef::substr方法的典型用法代码示例。如果您正苦于以下问题:C++ CStrRef::substr方法的具体用法?C++ CStrRef::substr怎么用?C++ CStrRef::substr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CStrRef
的用法示例。
在下文中一共展示了CStrRef::substr方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: parse_host
static void parse_host(CStrRef address, String &host, int &port) {
int pos = address.find(':');
if (pos >= 0) {
host = address.substr(0, pos);
port = address.substr(pos + 1).toInt16();
} else {
host = address;
port = 0;
}
}
示例2: f_putenv
bool f_putenv(CStrRef setting) {
int pos = setting.find('=');
if (pos >= 0) {
String name = setting.substr(0, pos);
String value = setting.substr(pos + 1);
g_context->setenv(name, value);
return true;
}
return false;
}
示例3: Explode
Variant StringUtil::Explode(CStrRef input, CStrRef delimiter,
int limit /* = 0x7FFFFFFF */) {
if (delimiter.empty()) {
throw_invalid_argument("delimiter: (empty)");
return false;
}
Array ret;
int pos = input.find(delimiter);
if (limit >= 0) {
if (pos < 0) {
ret.append(input);
} else {
int len = delimiter.size();
int pos0 = 0;
do {
ret.append(input.substr(pos0, pos - pos0));
pos += len;
pos0 = pos;
} while ((pos = input.find(delimiter, pos)) >= 0 && --limit > 1);
if (pos0 <= input.size()) {
ret.append(input.substr(pos0));
}
}
} else if (pos >= 0) {
vector<int> positions;
int len = delimiter.size();
int pos0 = 0;
int found = 0;
do {
positions.push_back(pos0);
positions.push_back(pos - pos0);
pos += len;
pos0 = pos;
found++;
} while ((pos = input.find(delimiter, pos)) >= 0);
if (pos0 <= input.size()) {
positions.push_back(pos0);
positions.push_back(input.size() - pos0);
found++;
}
int iMax = (found + limit) << 1;
for (int i = 0; i < iMax; i += 2) {
ret.append(input.substr(positions[i], positions[i+1]));
}
} // else we have negative limit and delimiter not found, returning empty arr
return ret;
}
示例4: parse_socket
static void parse_socket(CStrRef socket, String &protocol, String &host,
int &port) {
String address;
int pos = socket.find("://");
if (pos >= 0) {
protocol = socket.substr(0, pos);
address = socket.substr(pos + 3);
} else {
protocol = "tcp";
address = socket;
}
parse_host(address, host, port);
}
示例5: findFileWrapper
static bool findFileWrapper(CStrRef file, void* ctx) {
ResolveIncludeContext* context = (ResolveIncludeContext*)ctx;
assert(context->path.isNull());
Stream::Wrapper* w = Stream::getWrapperFromURI(file);
if (!dynamic_cast<FileStreamWrapper*>(w)) {
if (w->stat(file, context->s) == 0) {
context->path = file;
return true;
}
}
// handle file://
if (file.substr(0, 7) == s_file_url) {
return findFileWrapper(file.substr(7), ctx);
}
// TranslatePath() will canonicalize the path and also check
// whether the file is in an allowed directory.
String translatedPath = File::TranslatePathKeepRelative(file);
if (file[0] != '/') {
if (HPHP::Eval::FileRepository::findFile(translatedPath.get(),
context->s)) {
context->path = translatedPath;
return true;
}
return false;
}
if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) {
if (HPHP::Eval::FileRepository::findFile(translatedPath.get(),
context->s)) {
context->path = translatedPath;
return true;
}
}
string server_root(SourceRootInfo::GetCurrentSourceRoot());
if (server_root.empty()) {
server_root = string(g_vmContext->getCwd()->data());
if (server_root.empty() || server_root[server_root.size() - 1] != '/') {
server_root += "/";
}
}
String rel_path(Util::relativePath(server_root, translatedPath.data()));
if (HPHP::Eval::FileRepository::findFile(rel_path.get(),
context->s)) {
context->path = rel_path;
return true;
}
return false;
}
示例6: f_json_decode
Variant f_json_decode(CStrRef json, bool assoc /* = false */,
CVarRef options /* = 0 */) {
if (json.empty()) {
return uninit_null();
}
int64_t json_options = options.toInt64();
if (options.isBoolean() && options.toBooleanVal()) {
json_options = k_JSON_FB_LOOSE;
}
Variant z;
if (JSON_parser(z, json.data(), json.size(), assoc,
(json_options & k_JSON_FB_LOOSE))) {
return z;
}
if (json.size() == 4) {
if (!strcasecmp(json.data(), "null")) return uninit_null();
if (!strcasecmp(json.data(), "true")) return true;
} else if (json.size() == 5 && !strcasecmp(json.data(), "false")) {
return false;
}
int64_t p;
double d;
DataType type = json->isNumericWithVal(p, d, 0);
if (type == KindOfInt64) {
return p;
} else if (type == KindOfDouble) {
return d;
}
char ch0 = json.charAt(0);
if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') {
return json.substr(1, json.size() - 2);
}
if ((json_options & k_JSON_FB_LOOSE) && json.size() > 1 &&
ch0 == '\'' && json.charAt(json.size() - 1) == '\'') {
return json.substr(1, json.size() - 2);
}
if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */
return uninit_null();
}
return json;
}
示例7: splitHeader
bool Transport::splitHeader(CStrRef header, String &name, const char *&value) {
int pos = header.find(':');
if (pos != String::npos) {
name = header.substr(0, pos);
value = header.data() + pos;
do {
value++;
} while (*value == ' ');
return true;
}
// header("HTTP/1.0 404 Not Found");
// header("HTTP/1.0 404");
if (strncasecmp(header.data(), "http/", 5) == 0) {
int pos1 = header.find(' ');
if (pos1 != String::npos) {
int pos2 = header.find(' ', pos1 + 1);
if (pos2 == String::npos) pos2 = header.size();
if (pos2 - pos1 > 1) {
setResponse(atoi(header.data() + pos1),
getResponseInfo().empty() ? "splitHeader"
: getResponseInfo().c_str()
);
return false;
}
}
}
throw InvalidArgumentException("header", header.c_str());
}
示例8: f_stristr
Variant f_stristr(CStrRef haystack, CVarRef needle) {
Variant ret = f_stripos(haystack, needle);
if (same(ret, false)) {
return false;
}
return haystack.substr(ret.toInt32());
}
示例9: f_json_decode
Variant f_json_decode(CStrRef json, bool assoc /* = false */,
bool loose /* = false */) {
if (json.empty()) {
return null;
}
Variant z;
if (JSON_parser(z, json.data(), json.size(), assoc, loose)) {
return z;
}
if (json.size() == 4) {
if (!strcasecmp(json.data(), "null")) return null;
if (!strcasecmp(json.data(), "true")) return true;
} else if (json.size() == 5 && !strcasecmp(json.data(), "false")) {
return false;
}
int64 p;
double d;
DataType type = json->isNumericWithVal(p, d, 0);
if (type == KindOfInt64) {
return p;
} else if (type == KindOfDouble) {
return d;
}
char ch0 = json.charAt(0);
if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') {
return json.substr(1, json.size() - 2);
}
if (loose && json.size() > 1 &&
ch0 == '\'' && json.charAt(json.size() - 1) == '\'') {
return json.substr(1, json.size() - 2);
}
if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */
return null;
}
return json;
}
示例10: parse_host
// Extract host:port pair.
// 192.168.1.1:80 -> 192.168.1.0 80
// [2a03:2880::1]:80 -> [2a03:2880::1] 80
static void parse_host(CStrRef address, String &host, int &port) {
if (address.find('[') != std::string::npos) {
auto epos = address.rfind(']');
if (epos != std::string::npos) {
auto cpos = address.rfind(':', epos);
if (cpos != std::string::npos) {
port = address.substr(cpos + 1).toInt16();
}
}
host = address.substr(0, epos + 1);
} else {
auto cpos = address.rfind(':');
if (cpos != std::string::npos) {
host = address.substr(0, cpos);
port = address.substr(cpos + 1).toInt16();
} else {
host = address;
port = 0;
}
}
}
示例11: Split
Array StringUtil::Split(CStrRef str, int split_length /* = 1 */) {
if (split_length <= 0) {
throw InvalidArgumentException("split_length", "(non-positive)");
}
Array ret;
int len = str.size();
if (split_length >= len) {
ret.append(str);
} else {
for (int i = 0; i < len; i += split_length) {
ret.append(str.substr(i, split_length));
}
}
return ret;
}
示例12: Split
Variant StringUtil::Split(CStrRef str, int split_length /* = 1 */) {
if (split_length <= 0) {
throw_invalid_argument("split_length: (non-positive)");
return false;
}
Array ret;
int len = str.size();
if (split_length >= len) {
ret.append(str);
} else {
for (int i = 0; i < len; i += split_length) {
ret.append(str.substr(i, split_length));
}
}
return ret;
}
示例13: Split
Variant StringUtil::Split(CStrRef str, int split_length /* = 1 */) {
if (split_length <= 0) {
throw_invalid_argument(
"The length of each segment must be greater than zero"
);
return false;
}
Array ret;
int len = str.size();
if (split_length >= len) {
ret.append(str);
} else {
for (int i = 0; i < len; i += split_length) {
ret.append(str.substr(i, split_length));
}
}
return ret;
}
示例14: f_strcspn
Variant f_strcspn(CStrRef str1, CStrRef str2, int start /* = 0 */,
int length /* = 0x7FFFFFFF */) {
String s = str1.substr(start, length);
if (s.isNull()) return false;
return string_cspan(s, s.size(), str2, str2.size());
}