]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Reduce std::string::substr() usage
authorAttila Molnar <attilamolnar@hush.com>
Sat, 10 Jan 2015 14:16:03 +0000 (15:16 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Sat, 10 Jan 2015 14:16:03 +0000 (15:16 +0100)
substr() returns a new string while erase() and assign() modify the existing one

28 files changed:
src/channels.cpp
src/cidr.cpp
src/coremods/core_oper/cmd_rehash.cpp
src/coremods/core_whois.cpp
src/hashcomp.cpp
src/inspsocket.cpp
src/mode.cpp
src/modes/cmode_k.cpp
src/modules/extra/m_ssl_gnutls.cpp
src/modules/extra/m_ssl_openssl.cpp
src/modules/m_alias.cpp
src/modules/m_autoop.cpp
src/modules/m_chanhistory.cpp
src/modules/m_channelban.cpp
src/modules/m_dccallow.cpp
src/modules/m_exemptchanops.cpp
src/modules/m_httpd.cpp
src/modules/m_ldapauth.cpp
src/modules/m_namedmodes.cpp
src/modules/m_password_hash.cpp
src/modules/m_silence.cpp
src/modules/m_spanningtree/capab.cpp
src/modules/m_spanningtree/compat.cpp
src/modules/m_spanningtree/postcommand.cpp
src/modules/m_spanningtree/server.cpp
src/modules/m_spanningtree/treesocket1.cpp
src/modules/m_spanningtree/treesocket2.cpp
src/users.cpp

index 53a48c46970204d108f2eeba992bd6d429b296f2..fdf0f76e1abdbb4f20539b63749cda5546d3cfb6 100644 (file)
@@ -396,10 +396,10 @@ bool Channel::CheckBan(User* user, const std::string& mask)
                return false;
 
        const std::string nickIdent = user->nick + "!" + user->ident;
-       std::string prefix = mask.substr(0, at);
+       std::string prefix(mask, 0, at);
        if (InspIRCd::Match(nickIdent, prefix, NULL))
        {
-               std::string suffix = mask.substr(at + 1);
+               std::string suffix(mask, at + 1);
                if (InspIRCd::Match(user->host, suffix, NULL) ||
                        InspIRCd::Match(user->dhost, suffix, NULL) ||
                        InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
index 8d199353db58d069a9e48ab628e5e6f21db756d7..250ad9c69b0d716dc6cee3b2758fec1bede6b6e7 100644 (file)
@@ -53,8 +53,8 @@ bool irc::sockets::MatchCIDR(const std::string &address, const std::string &cidr
                }
                else
                {
-                       address_copy = address.substr(username_addr_pos + 1);
-                       cidr_copy = cidr_mask.substr(username_mask_pos + 1);
+                       address_copy.assign(address, username_addr_pos + 1, std::string::npos);
+                       cidr_copy.assign(cidr_mask, username_mask_pos + 1, std::string::npos);
                }
        }
        else
index 48dfa6fb14f2a19cfa45bdaa175579c6e8249d7f..19d2fa8c20260657c5a05b7e5c68971a5d3d0686 100644 (file)
@@ -55,7 +55,7 @@ CmdResult CommandRehash::Handle (const std::vector<std::string>& parameters, Use
 
                // the leading "-" is optional; remove it if present.
                if (param[0] == '-')
-                       param = param.substr(1);
+                       param.erase(param.begin());
 
                FOREACH_MOD(OnModuleRehash, (user, param));
                return CMD_SUCCESS;
index 966c4a790b23a17cb949dd191e37b534ef3361e6..7464e052753cad86ab0235389e32ad137d212d64 100644 (file)
@@ -96,7 +96,7 @@ void CommandWhois::SplitChanList(User* source, User* dest, const std::string& cl
                        line = prefix.str();
                }
 
-               line.append(cl.substr(start, pos - start + 1));
+               line.append(cl, start, pos - start + 1);
        }
 
        if (line.length() != prefix.str().length())
index 46981e7034d30c148bc5be264acc78733baa14c8..42e25f8f6a6248bfb0efc19294857cd409d34e19 100644 (file)
@@ -268,7 +268,7 @@ bool irc::tokenstream::GetToken(std::string &token)
        /* This is the last parameter */
        if (token[0] == ':' && !first)
        {
-               token = token.substr(1);
+               token.erase(token.begin());
                if (!StreamEnd())
                {
                        token += ' ';
@@ -332,7 +332,7 @@ bool irc::sepstream::GetToken(std::string &token)
        if (p == std::string::npos)
                p = this->tokens.length();
 
-       token = this->tokens.substr(this->pos, p - this->pos);
+       token.assign(tokens, this->pos, p - this->pos);
        this->pos = p + 1;
 
        return true;
@@ -412,10 +412,9 @@ long irc::portparser::GetToken()
        std::string::size_type dash = x.rfind('-');
        if (dash != std::string::npos)
        {
-               std::string sbegin = x.substr(0, dash);
-               std::string send = x.substr(dash+1, x.length());
+               std::string sbegin(x, 0, dash);
                range_begin = atoi(sbegin.c_str());
-               range_end = atoi(send.c_str());
+               range_end = atoi(x.c_str()+dash+1);
 
                if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
                {
index 494422075b2b8736e4edc4a1d1c7a3025cbccd73..886130f1bb556d894af729d6f782d46343ee255d 100644 (file)
@@ -150,9 +150,8 @@ bool StreamSocket::GetNextLine(std::string& line, char delim)
        std::string::size_type i = recvq.find(delim);
        if (i == std::string::npos)
                return false;
-       line = recvq.substr(0, i);
-       // TODO is this the most efficient way to split?
-       recvq = recvq.substr(i + 1);
+       line.assign(recvq, 0, i);
+       recvq.erase(0, i + 1);
        return true;
 }
 
@@ -299,7 +298,7 @@ void StreamSocket::DoWrite()
                                        else if (rv < itemlen)
                                        {
                                                SocketEngine::ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
-                                               front = front.substr(rv);
+                                               front.erase(0, rv);
                                                sendq_len -= rv;
                                                return;
                                        }
@@ -378,7 +377,7 @@ void StreamSocket::DoWrite()
                                        else
                                        {
                                                // stopped in the middle of this string
-                                               front = front.substr(rv);
+                                               front.erase(0, rv);
                                                rv = 0;
                                        }
                                }
index 335bb85ce5a065eb92edc1f7080a2ea59ca76dab..671b5d85456ddd44ec294a049c20d998e40c120b 100644 (file)
@@ -218,7 +218,7 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Mode
        std::string& parameter = mcitem.param;
        // crop mode parameter size to 250 characters
        if (parameter.length() > 250 && adding)
-               parameter = parameter.substr(0, 250);
+               parameter.erase(250);
 
        ModResult MOD_RESULT;
        FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
index 0f8070cb45bb09278ef94390e43ac95830df5445..6738c046e6b056471c6b1e95ad6c246a74909087 100644 (file)
@@ -52,7 +52,7 @@ ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, s
        channel->SetMode(this, adding);
        if (adding)
        {
-               parameter = parameter.substr(0, 32);
+               parameter.erase(32);
                ext.set(channel, parameter);
        }
        else
index 74b66d2de6bac523a23b9944b40a026d05a4b9d4..be642a79d65512187d49c00379662ffe1318691a 100644 (file)
@@ -909,7 +909,7 @@ info_done_dealloc:
                        }
                        else if (ret > 0)
                        {
-                               sendq = sendq.substr(ret);
+                               sendq.erase(0, ret);
                                SocketEngine::ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
                                return 0;
                        }
index a0c30bb9ee604bd10bb2fa75efefa526ad8eb0e6..b38478d6d992cb0429f6fc1c3db372cef1582949 100644 (file)
@@ -641,7 +641,7 @@ class OpenSSLIOHook : public SSLIOHook
                        }
                        else if (ret > 0)
                        {
-                               buffer = buffer.substr(ret);
+                               buffer.erase(0, ret);
                                SocketEngine::ChangeEventMask(user, FD_WANT_SINGLE_WRITE);
                                return 0;
                        }
index 20d3f5c45f8521e795edf5a2f52be7584c451aed..5b3979179bc0d39e665ea710693cbea277bc1a60 100644 (file)
@@ -148,7 +148,7 @@ class ModuleAlias : public Module
                        return MOD_RES_PASSTHRU;
 
                /* The parameters for the command in their original form, with the command stripped off */
-               std::string compare = original_line.substr(command.length());
+               std::string compare(original_line, command.length());
                while (*(compare.c_str()) == ' ')
                        compare.erase(compare.begin());
 
@@ -212,7 +212,7 @@ class ModuleAlias : public Module
                        return;
 
                /* The parameters for the command in their original form, with the command stripped off */
-               std::string compare = text.substr(scommand.length() + 1);
+               std::string compare(text, scommand.length() + 1);
                while (*(compare.c_str()) == ' ')
                        compare.erase(compare.begin());
 
index 801cf6c741747b4628c62b6f52679b91415746a8..2b9c2e1c21c28edfaf62adc6d3513a1ffa0ee08f 100644 (file)
@@ -47,7 +47,7 @@ class AutoOpList : public ListModeBase
                if (pos == 0 || pos == std::string::npos)
                        return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
                unsigned int mylevel = channel->GetPrefixValue(source);
-               std::string mid = parameter.substr(0, pos);
+               std::string mid(parameter, 0, pos);
                PrefixMode* mh = FindMode(mid);
 
                if (adding && !mh)
index f6e7ea40e6071a100203c576e880a744594e1f39..a0929a0d01c4c6f84e7f50a8b7653bf101bef643 100644 (file)
@@ -65,7 +65,7 @@ class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
                if (colon == std::string::npos)
                        return MODEACTION_DENY;
 
-               std::string duration = parameter.substr(colon+1);
+               std::string duration(parameter, colon+1);
                if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
                        return MODEACTION_DENY;
 
index 4d3f80e361d035cc155da3f7c9c7b8d47869ad37..189c0d0bcd895e4199e05ecb42a19d4bd6d32261 100644 (file)
@@ -32,12 +32,12 @@ class ModuleBadChannelExtban : public Module
        {
                if ((mask.length() > 2) && (mask[0] == 'j') && (mask[1] == ':'))
                {
-                       std::string rm = mask.substr(2);
+                       std::string rm(mask, 2);
                        char status = 0;
                        ModeHandler* mh = ServerInstance->Modes->FindPrefix(rm[0]);
                        if (mh)
                        {
-                               rm = mask.substr(3);
+                               rm.assign(mask, 3, std::string::npos);
                                status = mh->GetModeChar();
                        }
                        for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
index 1b838be5ce3fc62fbafbeef3964aa74944e77eb6..7627ba8c7d66d32c21e619531eea4ce259bb379b 100644 (file)
@@ -101,7 +101,7 @@ class CommandDccallow : public Command
                                }
                        }
 
-                       std::string nick = parameters[0].substr(1);
+                       std::string nick(parameters[0], 1);
                        User *target = ServerInstance->FindNickOnly(nick);
 
                        if ((target) && (!IS_SERVER(target)) && (!target->quitting) && (target->registered == REG_ALL))
index 2d06b73a0b64169c6ab707e51351e6ccd8ee4176..076445644ba2bfa84644d94eadff7b170a3ea5ed 100644 (file)
@@ -36,7 +36,7 @@ class ExemptChanOps : public ListModeBase
                        return false;
                }
 
-               std::string restriction = word.substr(0, p);
+               std::string restriction(word, 0, p);
                // If there is a '-' in the restriction string ignore it and everything after it
                // to support "auditorium-vis" and "auditorium-see" in m_auditorium
                p = restriction.find('-');
@@ -98,7 +98,7 @@ class ExemptHandler : public HandlerBase3<ModResult, User*, Channel*, const std:
                                if (pos == std::string::npos)
                                        continue;
                                if (!i->mask.compare(0, pos, restriction))
-                                       minmode = (*i).mask.substr(pos + 1);
+                                       minmode.assign(i->mask, pos + 1, std::string::npos);
                        }
                }
 
index a4452c9c09a1679adce6eff2a9ff0d798fdba373..bbd9f1275d98f365812009f6d25fcd4918c944d8 100644 (file)
@@ -269,7 +269,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
                                continue;
                        }
 
-                       std::string cheader = reqbuffer.substr(hbegin, hend - hbegin);
+                       std::string cheader(reqbuffer, hbegin, hend - hbegin);
 
                        std::string::size_type fieldsep = cheader.find(':');
                        if ((fieldsep == std::string::npos) || (fieldsep == 0) || (fieldsep == cheader.length() - 1))
@@ -300,7 +300,7 @@ class HttpServerSocket : public BufferedSocket, public Timer, public insp::intru
 
                        if (reqbuffer.length() >= postsize)
                        {
-                               postdata = reqbuffer.substr(0, postsize);
+                               postdata.assign(reqbuffer, 0, postsize);
                                reqbuffer.erase(0, postsize);
                        }
                        else if (!reqbuffer.empty())
index e89ce494981c02ff69fa3296536ec469e57f08f7..eee357ec0796dc2e7d290b5044e127340831e79d 100644 (file)
@@ -64,7 +64,7 @@ class BindInterface : public LDAPInterface
                                while (i < text.length() - 1 && isalpha(text[i + 1]))
                                        ++i;
 
-                               std::string key = text.substr(start, (i - start) + 1);
+                               std::string key(start, (i - start) + 1);
                                result.append(replacements[key]);
                        }
                        else
@@ -90,8 +90,8 @@ class BindInterface : public LDAPInterface
                                if (pos == std::string::npos) // malformed
                                        continue;
 
-                               std::string key = dnPart.substr(0, pos);
-                               std::string value = dnPart.substr(pos + 1, dnPart.length() - pos + 1); // +1s to skip the = itself
+                               std::string key(dnPart, 0, pos);
+                               std::string value(dnPart, pos + 1, dnPart.length() - pos + 1); // +1s to skip the = itself
                                dnParts[key] = value;
                        }
 
index 19dff1a8b6baa2e588e4e21b376f55e19e5907ad..1735df924298c4bdb70e5a6ef4d19574a5dc09ef 100644 (file)
@@ -138,8 +138,8 @@ class ModuleNamedModes : public Module
                                std::string::size_type eq = name.find('=');
                                if (eq != std::string::npos)
                                {
-                                       value = name.substr(eq + 1);
-                                       name = name.substr(0, eq);
+                                       value.assign(name, eq + 1, std::string::npos);
+                                       name.erase(eq);
                                }
 
                                ModeHandler* mh = ServerInstance->Modes->FindMode(name, MODETYPE_CHANNEL);
index 3cdd2b3ced385357ae5e1a9c2465e47de7665b0a..09cdbb402c180dd15e95cf15aabb75458f14af1d 100644 (file)
@@ -36,7 +36,7 @@ class CommandMkpasswd : public Command
        {
                if (!algo.compare(0, 5, "hmac-", 5))
                {
-                       std::string type = algo.substr(5);
+                       std::string type(algo, 5);
                        HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
                        if (!hp)
                        {
@@ -91,7 +91,7 @@ class ModuleOperHash : public Module
        {
                if (!hashtype.compare(0, 5, "hmac-", 5))
                {
-                       std::string type = hashtype.substr(5);
+                       std::string type(hashtype, 5);
                        HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
                        if (!hp)
                                return MOD_RES_PASSTHRU;
index 7298d77e1a87399d880770f49c2f8dd2d473640c..22de0ac8b6de76cf1ca9e6c4d2eb0626aaea6009 100644 (file)
@@ -134,7 +134,7 @@ class CommandSilence : public Command
                else if (parameters.size() > 0)
                {
                        // one or more parameters, add or delete entry from the list (only the first parameter is used)
-                       std::string mask = parameters[0].substr(1);
+                       std::string mask(parameters[0], 1);
                        char action = parameters[0][0];
                        // Default is private and notice so clients do not break
                        int pattern = CompilePattern("pn");
index f27fe8889f42611d9df7c211a4ca252c2a0b03a4..047808c2944e9155598e1ab6dfe71ff566d25ab6 100644 (file)
@@ -386,8 +386,8 @@ bool TreeSocket::Capab(const parameterlist &params)
                        std::string::size_type equals = item.find('=');
                        if (equals != std::string::npos)
                        {
-                               std::string var = item.substr(0, equals);
-                               std::string value = item.substr(equals+1, item.length());
+                               std::string var(item, 0, equals);
+                               std::string value(item, equals+1);
                                capab->CapKeys[var] = value;
                        }
                }
index 127907836020e72f1750d5d71fc548f07bfbf2bb..c2ee940fccb6e2e18fb73d5ba08defa4632cfa5e 100644 (file)
@@ -46,7 +46,7 @@ void TreeSocket::WriteLine(const std::string& original_line)
                        std::string line = original_line;
                        std::string::size_type a = line.find(' ');
                        std::string::size_type b = line.find(' ', a + 1);
-                       std::string command = line.substr(a + 1, b-a-1);
+                       std::string command(line, a + 1, b-a-1);
                        // now try to find a translation entry
                        // TODO a more efficient lookup method will be needed later
                        if (proto_version < 1205)
@@ -68,7 +68,7 @@ void TreeSocket::WriteLine(const std::string& original_line)
                                        {
                                                // No TS or modes in the command
                                                // :22DAAAAAB IJOIN #chan
-                                               const std::string channame = line.substr(b+1, c-b-1);
+                                               const std::string channame(line, b+1, c-b-1);
                                                Channel* chan = ServerInstance->FindChan(channame);
                                                if (!chan)
                                                        return;
index 0695ce63230fcd6069f2f0ceb89195508948a6a3..ae98be946520608313c18af76cbe8d61173986fc 100644 (file)
@@ -89,7 +89,7 @@ void SpanningTreeUtilities::RouteCommand(TreeServer* origin, CommandBase* thiscm
                if (ServerInstance->Modes->FindPrefix(dest[0]))
                {
                        pfx = dest[0];
-                       dest = dest.substr(1);
+                       dest.erase(dest.begin());
                }
                if (dest[0] == '#')
                {
index d7c6332f237f0766c45c8a3a82d11cfab9f8045b..1c624f5c4875e1ed8e35a73bf0765f7ca7865a38 100644 (file)
@@ -81,7 +81,7 @@ void CommandServer::HandleExtra(TreeServer* newserver, const std::vector<std::st
                if (p != std::string::npos)
                {
                        key.erase(p);
-                       val = prop.substr(p+1);
+                       val.assign(prop, p+1, std::string::npos);
                }
 
                if (key == "burst")
index 1a8bdd06c67055468a17ab301c6d338f12054d5f..d2fec0118a005f33a884e3eb97a4a161523b2607 100644 (file)
@@ -177,7 +177,7 @@ void TreeSocket::OnDataReady()
        {
                std::string::size_type rline = line.find('\r');
                if (rline != std::string::npos)
-                       line = line.substr(0,rline);
+                       line.erase(rline);
                if (line.find('\0') != std::string::npos)
                {
                        SendError("Read null character from socket");
index 25b8e3b71beaa7ea1301553d7bf70f00a1d803fd..1f98f78196277bc1491dddbb8a1745a373c91972 100644 (file)
@@ -47,7 +47,7 @@ void TreeSocket::Split(const std::string& line, std::string& prefix, std::string
 
        if (prefix[0] == ':')
        {
-               prefix = prefix.substr(1);
+               prefix.erase(prefix.begin());
 
                if (prefix.empty())
                {
index 9dcbcae0c9fbcc704f49cc9d1d52f5fd012ef60f..34986a1833073f4629fccff7b383871acc55185f 100644 (file)
@@ -277,7 +277,7 @@ void UserIOHandler::OnDataReady()
                return;
 eol_found:
                // just found a newline. Terminate the string, and pull it out of recvq
-               recvq = recvq.substr(qpos);
+               recvq.erase(0, qpos);
 
                // TODO should this be moved to when it was inserted in recvq?
                ServerInstance->stats.Recv += qpos;
@@ -764,7 +764,7 @@ void LocalUser::Write(const std::string& text)
        if (text.length() > ServerInstance->Config->Limits.MaxLine - 2)
        {
                // this should happen rarely or never. Crop the string at 512 and try again.
-               std::string try_again = text.substr(0, ServerInstance->Config->Limits.MaxLine - 2);
+               std::string try_again(0, ServerInstance->Config->Limits.MaxLine - 2);
                Write(try_again);
                return;
        }