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