2 // Copyright 2019 The Abseil Authors.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // https://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 #ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
17 #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
19 #include "absl/strings/match.h"
20 #include "absl/strings/string_view.h"
23 namespace flags_internal {
25 // A portable interface that returns the basename of the filename passed as an
26 // argument. It is similar to basename(3)
27 // <https://linux.die.net/man/3/basename>.
29 // flags_internal::Basename("a/b/prog/file.cc")
31 // flags_internal::Basename("file.cc")
33 inline absl::string_view Basename(absl::string_view filename) {
34 auto last_slash_pos = filename.find_last_of("/\\");
36 return last_slash_pos == absl::string_view::npos
38 : filename.substr(last_slash_pos + 1);
41 // A portable interface that returns the directory name of the filename
42 // passed as an argument, including the trailing slash.
43 // Returns the empty string if a slash is not found in the input file name.
45 // flags_internal::Package("a/b/prog/file.cc")
46 // returns "a/b/prog/"
47 // flags_internal::Package("file.cc")
49 inline absl::string_view Package(absl::string_view filename) {
50 auto last_slash_pos = filename.find_last_of("/\\");
52 return last_slash_pos == absl::string_view::npos
54 : filename.substr(0, last_slash_pos + 1);
57 } // namespace flags_internal
60 #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_