You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

path_util.h 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
  16. #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
  17. #include "absl/base/config.h"
  18. #include "absl/strings/string_view.h"
  19. namespace absl
  20. {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace flags_internal
  23. {
  24. // A portable interface that returns the basename of the filename passed as an
  25. // argument. It is similar to basename(3)
  26. // <https://linux.die.net/man/3/basename>.
  27. // For example:
  28. // flags_internal::Basename("a/b/prog/file.cc")
  29. // returns "file.cc"
  30. // flags_internal::Basename("file.cc")
  31. // returns "file.cc"
  32. inline absl::string_view Basename(absl::string_view filename)
  33. {
  34. auto last_slash_pos = filename.find_last_of("/\\");
  35. return last_slash_pos == absl::string_view::npos ? filename : filename.substr(last_slash_pos + 1);
  36. }
  37. // A portable interface that returns the directory name of the filename
  38. // passed as an argument, including the trailing slash.
  39. // Returns the empty string if a slash is not found in the input file name.
  40. // For example:
  41. // flags_internal::Package("a/b/prog/file.cc")
  42. // returns "a/b/prog/"
  43. // flags_internal::Package("file.cc")
  44. // returns ""
  45. inline absl::string_view Package(absl::string_view filename)
  46. {
  47. auto last_slash_pos = filename.find_last_of("/\\");
  48. return last_slash_pos == absl::string_view::npos ? absl::string_view() : filename.substr(0, last_slash_pos + 1);
  49. }
  50. } // namespace flags_internal
  51. ABSL_NAMESPACE_END
  52. } // namespace absl
  53. #endif // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_