当前位置: 首页>>代码示例>>C++>>正文


C++ string::substr方法代码示例

本文整理汇总了C++中exlib::string::substr方法的典型用法代码示例。如果您正苦于以下问题:C++ string::substr方法的具体用法?C++ string::substr怎么用?C++ string::substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在exlib::string的用法示例。


在下文中一共展示了string::substr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: readLine

result_t console_base::readLine(exlib::string msg, exlib::string &retVal,
                                AsyncEvent *ac)
{
    if (!ac)
        return CHECK_ERROR(CALL_E_LONGSYNC);

    flushLog();

#ifndef _WIN32
    static bool _init = false;
    static char *(*_readline)(const char *);
    static void (*_add_history)(char *);

#ifdef DEBUG
#ifdef __clang__
    static void (*_free)(void*);

    if (_free == 0)
        _free = (void (*)(void*))dlsym(RTLD_NEXT, "free");
#else
#define _free __real_free
#endif
#else
#define _free free
#endif

    if (!_init)
    {
        _init = true;

#ifdef __clang__
        void *handle = dlopen("libreadline.dylib", RTLD_LAZY);
#else
        const char *readline_dylib_names[] =
        {
            "libreadline.so.6",
            "libreadline.so.5",
            "libreadline.so"
        };
        const size_t readline_dylib_names_size = ARRAYSIZE(readline_dylib_names);
        void *handle = 0;

        for (size_t i = 0; i < readline_dylib_names_size; i++)
        {
            handle = dlopen(readline_dylib_names[i], RTLD_LAZY);
            if (handle) break;
        }
#endif

        if (handle)
        {
            _readline = (char *(*)(const char *))dlsym(handle, "readline");
            _add_history = (void (*)(char *))dlsym(handle, "add_history");
        } else
        {
            _readline = readline;
            _add_history = add_history;
        }
    }

    if (isatty(fileno(stdin)))
    {
        char *line;
        const char* lfptr = qstrrchr(msg.c_str(), '\n');

        if (lfptr != NULL)
        {
            puts(msg.substr(0, lfptr - msg.c_str()).c_str());
            line = _readline(lfptr + 1);
        }
        else
            line = _readline(msg.c_str());

        if (!line)
            return CHECK_ERROR(LastError());

        if (*line)
        {
            _add_history(line);
            retVal = line;
        }
        _free(line);
    } else
#endif
    {
        std_logger::out(msg);
        char *line = read_line();

        if (!line)
            return CHECK_ERROR(LastError());

        retVal = line;
        free(line);
    }

    return 0;
}
开发者ID:ngot,项目名称:fibjs,代码行数:97,代码来源:console.cpp

示例2: trimUrl

void Url::trimUrl(exlib::string url, exlib::string& retVal)
{
    exlib::string rest;
    int32_t start = -1;
    int32_t end = -1;
    int32_t lastPos = 0;
    int32_t i;
    bool isWs;

    bool inWs = false;

    for (i = 0; i < (int32_t)url.length(); i++) {
        isWs = url[i] == 32 || url[i] == 9 || url[i] == 13 || url[i] == 10 || url[i] == 12;

        if (*(unsigned char*)&url[i] == 0xc2 && *(unsigned char*)&url[i + 1] == 0xa0) {
            isWs = true;
            i++;
        }
        if (*(unsigned char*)&url[i] == 239 && *(unsigned char*)&url[i + 1] == 187 && *(unsigned char*)&url[i + 2] == 191) {
            isWs = true;
            i += 2;
        }

        if (start == -1) {
            if (isWs)
                continue;
            lastPos = start = i;
        } else {
            if (inWs) {
                if (!isWs) {
                    end = -1;
                    inWs = false;
                }
            } else if (isWs) {
                end = i;
                inWs = true;
            }
        }
        if (url[i] == 92 && i - lastPos > 0)
            url[i] = '/';
    }

    if (start != -1) {
        if (lastPos == start) {
            if (end == -1) {
                if (start == 0)
                    rest = url;
                else
                    rest = url.substr(start);
            } else {
                rest = url.substr(start, end - start);
            }
        } else if (end == -1 && lastPos < (int32_t)url.length()) {
            // We converted some backslashes and have only part of the entire string
            rest = url.substr(lastPos);
        } else if (end != -1 && lastPos < end) {
            // We converted some backslashes and have only part of the entire string
            rest = url.substr(lastPos, end);
        }
    }
    retVal = rest;
}
开发者ID:asionius,项目名称:fibjs,代码行数:62,代码来源:Url.cpp


注:本文中的exlib::string::substr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。