summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Vogt <mvo@debian.org>2013-11-29 08:35:05 +0100
committerMichael Vogt <mvo@debian.org>2013-11-29 08:37:54 +0100
commitcf993341c2067ee091cfd51e5da0e237babce171 (patch)
tree1ece0eaaaea76acf5fba1c74054873d80ef633b5
parenta8f671c17a7f34e80a49bf367bceaa009551b066 (diff)
add "APT::String::Endswith" and automatic adding of ".list" in apt edit-source
-rw-r--r--apt-pkg/contrib/strutl.cc8
-rw-r--r--apt-pkg/contrib/strutl.h1
-rw-r--r--apt-private/private-sources.cc10
-rw-r--r--test/libapt/strutil_test.cc18
4 files changed, 34 insertions, 3 deletions
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index 9f794927d..962112854 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/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);
+}
+
}
}
/*}}}*/
diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h
index c8fc317c0..8d746f10e 100644
--- a/apt-pkg/contrib/strutl.h
+++ b/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);
};
};
diff --git a/apt-private/private-sources.cc b/apt-private/private-sources.cc
index 37b0534bd..65706e785 100644
--- a/apt-private/private-sources.cc
+++ b/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();
diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc
index 110a20d27..8215654d0 100644
--- a/test/libapt/strutil_test.cc
+++ b/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;
}