From 404528bd581a4d2fa3bae1834d6fde48c6153434 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 21 Sep 2011 18:42:08 +0200 Subject: convert a few for-loop char finds to proper strchr and memchr --- apt-pkg/acquire-method.cc | 10 +++++----- apt-pkg/contrib/cmndline.cc | 28 +++++++++++----------------- apt-pkg/contrib/strutl.cc | 15 +++++++-------- apt-pkg/deb/deblistparser.cc | 21 ++++++++------------- apt-pkg/deb/debversion.cc | 25 ++++++++++--------------- apt-pkg/sourcelist.cc | 2 +- 6 files changed, 42 insertions(+), 59 deletions(-) diff --git a/apt-pkg/acquire-method.cc b/apt-pkg/acquire-method.cc index 7e9061e56..294d78f86 100644 --- a/apt-pkg/acquire-method.cc +++ b/apt-pkg/acquire-method.cc @@ -285,12 +285,12 @@ bool pkgAcqMethod::Configuration(string Message) I += Length + 1; for (; I < MsgEnd && *I == ' '; I++); - const char *Equals = I; - for (; Equals < MsgEnd && *Equals != '='; Equals++); - const char *End = Equals; - for (; End < MsgEnd && *End != '\n'; End++); - if (End == Equals) + const char *Equals = (const char*) memchr(I, '=', MsgEnd - I); + if (Equals == NULL) return false; + const char *End = (const char*) memchr(Equals, '\n', MsgEnd - Equals); + if (End == NULL) + End = MsgEnd; Cnf.Set(DeQuoteString(string(I,Equals-I)), DeQuoteString(string(Equals+1,End-Equals-1))); diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index 5a9944096..f7359c36e 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -87,9 +87,8 @@ bool CommandLine::Parse(int argc,const char **argv) Opt++; // Match up to a = against the list - const char *OptEnd = Opt; Args *A; - for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++); + const char *OptEnd = strchrnul(Opt, '='); for (A = ArgList; A->end() == false && stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); @@ -97,9 +96,8 @@ bool CommandLine::Parse(int argc,const char **argv) bool PreceedMatch = false; if (A->end() == true) { - for (; Opt != OptEnd && *Opt != '-'; Opt++); - - if (Opt == OptEnd) + Opt = (const char*) memchr(Opt, '-', OptEnd - Opt); + if (Opt == NULL) return _error->Error(_("Command line option %s is not understood"),argv[I]); Opt++; @@ -194,9 +192,8 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], // Arbitrary item specification if ((A->Flags & ArbItem) == ArbItem) { - const char *J; - for (J = Argument; *J != 0 && *J != '='; J++); - if (*J == 0) + const char *J = strchr(Argument, '='); + if (J == NULL) return _error->Error(_("Option %s: Configuration item specification must have an =."),argv[I]); // = is trailing @@ -212,8 +209,7 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], return true; } - const char *I = A->ConfName; - for (; *I != 0 && *I != ' '; I++); + const char *I = strchrnul(A->ConfName, ' '); if (*I == ' ') Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument); else @@ -269,10 +265,9 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], // Skip the leading dash const char *J = argv[I]; for (; *J != 0 && *J == '-'; J++); - - const char *JEnd = J; - for (; *JEnd != 0 && *JEnd != '-'; JEnd++); - if (*JEnd != 0) + + const char *JEnd = strchr(J, '-'); + if (JEnd != NULL) { strncpy(Buffer,J,JEnd - J); Buffer[JEnd - J] = 0; @@ -373,9 +368,8 @@ void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * co { // That is possibly an option: Quote it if it includes spaces, // the benefit is that this will eliminate also most false positives - const char* c = &argv[i][j+1]; - for (; *c != '\0' && *c != ' '; ++c); - if (*c == '\0') continue; + const char* c = strchr(&argv[i][j+1], ' '); + if (c == NULL) continue; cmdline[++length] = '"'; closeQuote = true; } diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 867bb313b..8dd05b9c0 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -179,14 +179,14 @@ bool ParseQuoteWord(const char *&String,string &Res) { if (*C == '"') { - for (C++; *C != 0 && *C != '"'; C++); - if (*C == 0) + C = strchr(C + 1, '"'); + if (C == NULL) return false; } if (*C == '[') { - for (C++; *C != 0 && *C != ']'; C++); - if (*C == 0) + C = strchr(C + 1, ']'); + if (C == NULL) return false; } } @@ -904,11 +904,10 @@ bool StrToTime(const string &Val,time_t &Result) { struct tm Tm; char Month[10]; - const char *I = Val.c_str(); - + // Skip the day of the week - for (;*I != 0 && *I != ' '; I++); - + const char *I = strchr(Val.c_str(), ' '); + // Handle RFC 1123 time Month[0] = 0; if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year, diff --git a/apt-pkg/deb/deblistparser.cc b/apt-pkg/deb/deblistparser.cc index 8d3f6f0ba..0562be44c 100644 --- a/apt-pkg/deb/deblistparser.cc +++ b/apt-pkg/deb/deblistparser.cc @@ -525,9 +525,9 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, // Skip whitespace for (;I != Stop && isspace(*I) != 0; I++); Start = I; - for (;I != Stop && *I != ')'; I++); - if (I == Stop || Start == I) - return 0; + I = (const char*) memchr(I, ')', Stop - I); + if (I == NULL || Start == I) + return 0; // Skip trailing whitespace const char *End = I; @@ -800,21 +800,16 @@ bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI, } // seperate the tag from the data - for (; buffer[len] != ':' && buffer[len] != '\0'; ++len) - /* nothing */ - ; - if (buffer[len] == '\0') + const char* dataStart = strchr(buffer + len, ':'); + if (dataStart == NULL) continue; - char* dataStart = buffer + len; + len = dataStart - buffer; for (++dataStart; *dataStart == ' '; ++dataStart) /* nothing */ ; - char* dataEnd = dataStart; - for (++dataEnd; *dataEnd != '\0'; ++dataEnd) - /* nothing */ - ; + const char* dataEnd = (const char*)rawmemchr(dataStart, '\0'); // The last char should be a newline, but we can never be sure: #633350 - char* lineEnd = dataEnd; + const char* lineEnd = dataEnd; for (--lineEnd; *lineEnd == '\r' || *lineEnd == '\n'; --lineEnd) /* nothing */ ; diff --git a/apt-pkg/deb/debversion.cc b/apt-pkg/deb/debversion.cc index 755ffbe96..340403721 100644 --- a/apt-pkg/deb/debversion.cc +++ b/apt-pkg/deb/debversion.cc @@ -127,14 +127,12 @@ int debVersioningSystem::CmpFragment(const char *A,const char *AEnd, int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd, const char *B,const char *BEnd) { - // Strip off the epoch and compare it - const char *lhs = A; - const char *rhs = B; - for (;lhs != AEnd && *lhs != ':'; lhs++); - for (;rhs != BEnd && *rhs != ':'; rhs++); - if (lhs == AEnd) + // Strip off the epoch and compare it + const char *lhs = (const char*) memchr(A, ':', AEnd - A); + const char *rhs = (const char*) memchr(B, ':', BEnd - B); + if (lhs == NULL) lhs = A; - if (rhs == BEnd) + if (rhs == NULL) rhs = B; // Special case: a zero epoch is the same as no epoch, @@ -169,15 +167,12 @@ int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd, if (rhs != B) rhs++; - // Find the last - - const char *dlhs = AEnd-1; - const char *drhs = BEnd-1; - for (;dlhs > lhs && *dlhs != '-'; dlhs--); - for (;drhs > rhs && *drhs != '-'; drhs--); - - if (dlhs == lhs) + // Find the last - + const char *dlhs = (const char*) memrchr(lhs, '-', AEnd - lhs); + const char *drhs = (const char*) memrchr(rhs, '-', BEnd - rhs); + if (dlhs == NULL) dlhs = AEnd; - if (drhs == rhs) + if (drhs == NULL) drhs = BEnd; // Compare the main version diff --git a/apt-pkg/sourcelist.cc b/apt-pkg/sourcelist.cc index a25358bf2..ebfb5289e 100644 --- a/apt-pkg/sourcelist.cc +++ b/apt-pkg/sourcelist.cc @@ -266,7 +266,7 @@ bool pkgSourceList::ReadAppend(string File) // CNC:2003-02-20 - Do not break if '#' is inside []. for (I = Buffer; *I != 0 && *I != '#'; I++) if (*I == '[') - for (I++; *I != 0 && *I != ']'; I++); + I = strchr(I + 1, ']'); *I = 0; const char *C = _strstrip(Buffer); -- cgit v1.2.3