From e7c33f14dc0ae7b43b21db16fd0ce48934049a57 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 3 Jun 2007 21:43:11 -0300 Subject: [PATCH] add File_name::canonicalized() - add string_join() - do the right thing with a//b for string_split() - test functions --- flower/file-name.cc | 23 +++++++++++++++++++++++ flower/include/file-name.hh | 2 +- flower/include/std-vector.hh | 1 + flower/std-string.cc | 20 ++++++++++++++++++-- flower/test-file-name.cc | 7 +++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/flower/file-name.cc b/flower/file-name.cc index e5736e06b0..0f0bb61270 100644 --- a/flower/file-name.cc +++ b/flower/file-name.cc @@ -177,3 +177,26 @@ File_name::is_absolute () const return (dir_.length () && dir_[0] == DIRSEP) || root_.length (); } + + +File_name +File_name::canonicalized () const +{ + File_name c = *this; + + replace_all (c.dir_, string ("//"), string ("/")); + + vector components = string_split (c.dir_, '/'); + vector new_components; + + for (vsize i = 0; i < components.size (); i++) + { + if (components[i] == "..") + new_components.pop_back (); + else + new_components.push_back (components[i]); + } + + c.dir_ = string_join (new_components, "/"); + return c; +} diff --git a/flower/include/file-name.hh b/flower/include/file-name.hh index e8ea1ea3c2..bd699e0b31 100644 --- a/flower/include/file-name.hh +++ b/flower/include/file-name.hh @@ -27,7 +27,7 @@ public: bool is_absolute () const; string to_string () const; - + File_name canonicalized () const; string dir_part () const; string file_part () const; }; diff --git a/flower/include/std-vector.hh b/flower/include/std-vector.hh index 4c3dcc0e0e..5c2c09c9d0 100644 --- a/flower/include/std-vector.hh +++ b/flower/include/std-vector.hh @@ -258,6 +258,7 @@ junk_pointers (vector &v) #endif /* HAVE_BOOST_LAMBDA */ vector string_split (string str, char c); +string string_join (vector const &strs, string infix); #define iterof(i,s) typeof((s).begin()) i((s).begin()) diff --git a/flower/std-string.cc b/flower/std-string.cc index b78b019661..1221609c6c 100644 --- a/flower/std-string.cc +++ b/flower/std-string.cc @@ -67,6 +67,9 @@ to_string (char const *format, ...) return str; } +/* + TODO: this O(n^2) in #occurences of find, due to repeated copying. + */ string & replace_all (string &str, string find, string replace) { @@ -113,8 +116,7 @@ string_split (string str, char c) { string s = str.substr (0, i); a.push_back (s); - while (str[++i] == c) - ; + i ++; str = str.substr (i); i = str.find (c); } @@ -122,3 +124,17 @@ string_split (string str, char c) a.push_back (str); return a; } + +string +string_join (vector const &strs, string infix) +{ + string result; + for (vsize i = 0; i < strs.size (); i ++) + { + if (i) + result += infix; + result += strs[i]; + } + + return result; +} diff --git a/flower/test-file-name.cc b/flower/test-file-name.cc index edae635e82..9bd06f121c 100644 --- a/flower/test-file-name.cc +++ b/flower/test-file-name.cc @@ -34,3 +34,10 @@ TEST_STRING (File_name, Mingw_slashify_4, "\\tmp\\x.ly") string s = slashify (to_string ()); EQUAL ("/tmp/x.ly", s); } + +TEST_STRING (File_name, Canonicalize, "foo//bar/..//bla//z.ly") +{ + string s = canonicalized ().to_string (); + EQUAL ("foo/bla/z.ly", s); +} + -- 2.39.2