Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright 2019 The Abseil Authors. |
| 3 | // |
| 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 |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 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. |
| 15 | |
| 16 | #ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ |
| 17 | #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ |
| 18 | |
| 19 | #include "absl/strings/match.h" |
| 20 | #include "absl/strings/string_view.h" |
| 21 | |
| 22 | namespace absl { |
| 23 | namespace flags_internal { |
| 24 | |
| 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>. |
| 28 | // For example: |
| 29 | // flags_internal::Basename("a/b/prog/file.cc") |
| 30 | // returns "file.cc" |
| 31 | // flags_internal::Basename("file.cc") |
| 32 | // returns "file.cc" |
| 33 | inline absl::string_view Basename(absl::string_view filename) { |
| 34 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 35 | |
| 36 | return last_slash_pos == absl::string_view::npos |
| 37 | ? filename |
| 38 | : filename.substr(last_slash_pos + 1); |
| 39 | } |
| 40 | |
| 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. |
| 44 | // For example: |
| 45 | // flags_internal::Package("a/b/prog/file.cc") |
| 46 | // returns "a/b/prog/" |
| 47 | // flags_internal::Package("file.cc") |
| 48 | // returns "" |
| 49 | inline absl::string_view Package(absl::string_view filename) { |
| 50 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 51 | |
| 52 | return last_slash_pos == absl::string_view::npos |
| 53 | ? absl::string_view() |
| 54 | : filename.substr(0, last_slash_pos + 1); |
| 55 | } |
| 56 | |
| 57 | } // namespace flags_internal |
| 58 | } // namespace absl |
| 59 | |
| 60 | #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ |