StringView.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2017 Google
  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. * http://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 IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_
  17. #define IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_
  18. #import <Foundation/Foundation.h>
  19. #include <string>
  20. #include "absl/strings/string_view.h"
  21. #include "leveldb/slice.h"
  22. namespace Firestore {
  23. // A simple wrapper for the character data of any string-like type to which
  24. // we'd like to temporarily refer as an argument.
  25. //
  26. // This is superficially similar to StringPiece and leveldb::Slice except
  27. // that it also supports implicit conversion from NSString *, which is useful
  28. // when writing Objective-C++ methods that accept any string-like type.
  29. //
  30. // Note that much like any other view-type class in C++, the caller is
  31. // responsible for ensuring that the lifetime of the string-like data is longer
  32. // than the lifetime of the StringView.
  33. //
  34. // Functions that take a StringView argument promise that they won't keep the
  35. // pointer beyond the immediate scope of their own stack frame.
  36. class StringView {
  37. public:
  38. // Creates a StringView from an NSString. When StringView is an argument type
  39. // into which an NSString* is passed, the caller should ensure that the
  40. // NSString is retained.
  41. StringView(NSString *str) // NOLINT(runtime/explicit)
  42. : data_([str UTF8String]), size_([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) {
  43. }
  44. // Creates a StringView from the given char* pointer with an explicit size.
  45. // The character data can contain NUL bytes as a result.
  46. StringView(const char *data, size_t size) : data_(data), size_(size) {
  47. }
  48. // Creates a StringView from the given char* pointer but computes the size
  49. // with strlen. This is really only suitable for passing C string literals.
  50. StringView(const char *data) // NOLINT(runtime/explicit)
  51. : data_(data), size_(strlen(data)) {
  52. }
  53. // Creates a StringView from the given slice.
  54. StringView(leveldb::Slice slice) // NOLINT(runtime/explicit)
  55. : data_(slice.data()), size_(slice.size()) {
  56. }
  57. // Creates a StringView from the absl::string_view.
  58. StringView(absl::string_view s) // NOLINT(runtime/explicit)
  59. : data_(s.data()), size_(s.size()) {
  60. }
  61. // Creates a StringView from the given std::string. The string must be an
  62. // lvalue for the lifetime requirements to be satisfied.
  63. StringView(const std::string &str) // NOLINT(runtime/explicit)
  64. : data_(str.data()), size_(str.size()) {
  65. }
  66. // Converts this StringView to a Slice, which is an equivalent (and more
  67. // functional) type. The returned slice has the same lifetime as this
  68. // StringView.
  69. operator leveldb::Slice() {
  70. return leveldb::Slice(data_, size_);
  71. }
  72. // Converts this StringView to a absl::string_view, which is an equivalent (and more
  73. // functional) type. The returned string_view has the same lifetime as this
  74. // StringView.
  75. operator absl::string_view() {
  76. return absl::string_view(data_, size_);
  77. }
  78. private:
  79. const char *data_;
  80. const size_t size_;
  81. };
  82. } // namespace Firestore
  83. #endif // IPHONE_FIRESTORE_SOURCE_LOCAL_STRING_VIEW_H_