summaryrefslogtreecommitdiffstats
path: root/xpcom/string/nsTDependentSubstring.h
blob: b5198ff2b5001b72704df765b4fc72af197db254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// IWYU pragma: private, include "nsString.h"

#ifndef nsTDependentSubstring_h
#define nsTDependentSubstring_h

#include "nsTSubstring.h"
#include "nsTLiteralString.h"
#include "mozilla/Span.h"

/**
 * nsTDependentSubstring_CharT
 *
 * A string class which wraps an external array of string characters. It
 * is the client code's responsibility to ensure that the external buffer
 * remains valid for a long as the string is alive.
 *
 * NAMES:
 *   nsDependentSubstring for wide characters
 *   nsDependentCSubstring for narrow characters
 */
template <typename T>
class nsTDependentSubstring : public nsTSubstring<T> {
 public:
  typedef nsTDependentSubstring<T> self_type;
  typedef nsTSubstring<T> substring_type;
  typedef typename substring_type::fallible_t fallible_t;

  typedef typename substring_type::char_type char_type;
  typedef typename substring_type::char_traits char_traits;
  typedef
      typename substring_type::incompatible_char_type incompatible_char_type;

  typedef typename substring_type::substring_tuple_type substring_tuple_type;

  typedef typename substring_type::const_iterator const_iterator;
  typedef typename substring_type::iterator iterator;

  typedef typename substring_type::comparator_type comparator_type;

  typedef typename substring_type::const_char_iterator const_char_iterator;

  typedef typename substring_type::string_view string_view;

  typedef typename substring_type::index_type index_type;
  typedef typename substring_type::size_type size_type;

  // These are only for internal use within the string classes:
  typedef typename substring_type::DataFlags DataFlags;
  typedef typename substring_type::ClassFlags ClassFlags;

 public:
  void Rebind(const substring_type&, size_type aStartPos,
              size_type aLength = size_type(-1));

  void Rebind(const char_type* aData, size_type aLength);

  void Rebind(const char_type* aStart, const char_type* aEnd);

  nsTDependentSubstring(const substring_type& aStr, size_type aStartPos,
                        size_type aLength = size_type(-1))
      : substring_type() {
    Rebind(aStr, aStartPos, aLength);
  }

  nsTDependentSubstring(const char_type* aData, size_type aLength)
      : substring_type(const_cast<char_type*>(aData), aLength, DataFlags(0),
                       ClassFlags(0)) {}

  explicit nsTDependentSubstring(mozilla::Span<const char_type> aData)
      : nsTDependentSubstring(aData.Elements(), aData.Length()) {}

  nsTDependentSubstring(const char_type* aStart, const char_type* aEnd);

#if defined(MOZ_USE_CHAR16_WRAPPER)
  template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
  nsTDependentSubstring(char16ptr_t aData, size_type aLength)
      : nsTDependentSubstring(static_cast<const char16_t*>(aData), aLength) {}

  template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
  nsTDependentSubstring(char16ptr_t aStart, char16ptr_t aEnd);
#endif

  nsTDependentSubstring(const const_iterator& aStart,
                        const const_iterator& aEnd);

  // Create a nsTDependentSubstring to be bound later
  nsTDependentSubstring() : substring_type() {}

  // auto-generated copy-constructor OK (XXX really?? what about base class
  // copy-ctor?)
  nsTDependentSubstring(const nsTDependentSubstring&) = default;

 private:
  // NOT USED
  void operator=(const self_type&) =
      delete;  // we're immutable, you can't assign into a substring
};

extern template class nsTDependentSubstring<char>;
extern template class nsTDependentSubstring<char16_t>;

template <typename T>
inline const nsTDependentSubstring<T> Substring(const nsTSubstring<T>& aStr,
                                                size_t aStartPos,
                                                size_t aLength = size_t(-1)) {
  return nsTDependentSubstring<T>(aStr, aStartPos, aLength);
}

template <typename T>
inline const nsTDependentSubstring<T> Substring(const nsTLiteralString<T>& aStr,
                                                size_t aStartPos,
                                                size_t aLength = size_t(-1)) {
  return nsTDependentSubstring<T>(aStr, aStartPos, aLength);
}

template <typename T>
inline const nsTDependentSubstring<T> Substring(
    const nsReadingIterator<T>& aStart, const nsReadingIterator<T>& aEnd) {
  return nsTDependentSubstring<T>(aStart.get(), aEnd.get());
}

template <typename T>
inline const nsTDependentSubstring<T> Substring(const T* aData,
                                                size_t aLength) {
  return nsTDependentSubstring<T>(aData, aLength);
}

template <typename T>
const nsTDependentSubstring<T> Substring(const T* aStart, const T* aEnd);

extern template const nsTDependentSubstring<char> Substring(const char* aStart,
                                                            const char* aEnd);

extern template const nsTDependentSubstring<char16_t> Substring(
    const char16_t* aStart, const char16_t* aEnd);

#if defined(MOZ_USE_CHAR16_WRAPPER)
inline const nsTDependentSubstring<char16_t> Substring(char16ptr_t aData,
                                                       size_t aLength);

const nsTDependentSubstring<char16_t> Substring(char16ptr_t aStart,
                                                char16ptr_t aEnd);
#endif

template <typename T>
inline const nsTDependentSubstring<T> StringHead(const nsTSubstring<T>& aStr,
                                                 size_t aCount) {
  return nsTDependentSubstring<T>(aStr, 0, aCount);
}

template <typename T>
inline const nsTDependentSubstring<T> StringTail(const nsTSubstring<T>& aStr,
                                                 size_t aCount) {
  return nsTDependentSubstring<T>(aStr, aStr.Length() - aCount, aCount);
}

#endif