Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:12

0001 // Copyright 2022 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_CORD_DATA_EDGE_H_
0016 #define ABSL_STRINGS_INTERNAL_CORD_DATA_EDGE_H_
0017 
0018 #include <cassert>
0019 #include <cstddef>
0020 
0021 #include "absl/base/config.h"
0022 #include "absl/strings/internal/cord_internal.h"
0023 #include "absl/strings/internal/cord_rep_flat.h"
0024 #include "absl/strings/string_view.h"
0025 
0026 namespace absl {
0027 ABSL_NAMESPACE_BEGIN
0028 namespace cord_internal {
0029 
0030 // Returns true if the provided rep is a FLAT, EXTERNAL or a SUBSTRING node
0031 // holding a FLAT or EXTERNAL child rep. Requires `rep != nullptr`.
0032 inline bool IsDataEdge(const CordRep* edge) {
0033   assert(edge != nullptr);
0034 
0035   // The fast path is that `edge` is an EXTERNAL or FLAT node, making the below
0036   // if a single, well predicted branch. We then repeat the FLAT or EXTERNAL
0037   // check in the slow path of the SUBSTRING check to optimize for the hot path.
0038   if (edge->tag == EXTERNAL || edge->tag >= FLAT) return true;
0039   if (edge->tag == SUBSTRING) edge = edge->substring()->child;
0040   return edge->tag == EXTERNAL || edge->tag >= FLAT;
0041 }
0042 
0043 // Returns the `absl::string_view` data reference for the provided data edge.
0044 // Requires 'IsDataEdge(edge) == true`.
0045 inline absl::string_view EdgeData(const CordRep* edge) {
0046   assert(IsDataEdge(edge));
0047 
0048   size_t offset = 0;
0049   const size_t length = edge->length;
0050   if (edge->IsSubstring()) {
0051     offset = edge->substring()->start;
0052     edge = edge->substring()->child;
0053   }
0054   return edge->tag >= FLAT
0055              ? absl::string_view{edge->flat()->Data() + offset, length}
0056              : absl::string_view{edge->external()->base + offset, length};
0057 }
0058 
0059 }  // namespace cord_internal
0060 ABSL_NAMESPACE_END
0061 }  // namespace absl
0062 
0063 #endif  // ABSL_STRINGS_INTERNAL_CORD_DATA_EDGE_H_