Browse Source

add "APT::String::Endswith" and automatic adding of ".list" in apt edit-source

debian/1.8.y
Michael Vogt 10 years ago
parent
commit
cf993341c2
  1. 8
      apt-pkg/contrib/strutl.cc
  2. 1
      apt-pkg/contrib/strutl.h
  3. 10
      apt-private/private-sources.cc
  4. 18
      test/libapt/strutil_test.cc

8
apt-pkg/contrib/strutl.cc

@ -49,6 +49,14 @@ std::string Strip(const std::string &s)
size_t end = s.find_last_not_of(" \t\n");
return s.substr(start, end-start+1);
}
bool Endswith(const std::string &s, const std::string &end)
{
if (end.size() > s.size())
return false;
return (s.substr(s.size() - end.size(), s.size()) == end);
}
}
}
/*}}}*/

1
apt-pkg/contrib/strutl.h

@ -36,6 +36,7 @@ using std::ostream;
namespace APT {
namespace String {
std::string Strip(const std::string &s);
bool Endswith(const std::string &s, const std::string &ending);
};
};

10
apt-private/private-sources.cc

@ -21,10 +21,13 @@ bool EditSources(CommandLine &CmdL)
std::string sourceslist;
if (CmdL.FileList[1] != NULL)
sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1] + ".list";
else
{
sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1];
if (!APT::String::Endswith(sourceslist, ".list"))
sourceslist += ".list";
} else {
sourceslist = _config->FindFile("Dir::Etc::sourcelist");
}
HashString before;
if (FileExists(sourceslist))
before.FromFile(sourceslist);
@ -38,6 +41,7 @@ bool EditSources(CommandLine &CmdL)
strprintf(outs, _("Failed to parse %s. Edit again? "),
sourceslist.c_str());
std::cout << outs;
// FIXME: should we add a "restore previous" option here?
res = !YnPrompt(true);
}
_error->RevertToStack();

18
test/libapt/strutil_test.cc

@ -69,5 +69,23 @@ int main(int argc,char *argv[])
result = StringSplit(input, "");
equals(result.size(), 0);
// endswith
bool b;
input = "abcd";
b = APT::String::Endswith(input, "d");
equals(b, true);
b = APT::String::Endswith(input, "cd");
equals(b, true);
b = APT::String::Endswith(input, "abcd");
equals(b, true);
b = APT::String::Endswith(input, "x");
equals(b, false);
b = APT::String::Endswith(input, "abcndefg");
equals(b, false);
return 0;
}

Loading…
Cancel
Save