OpenMS
Loading...
Searching...
No Matches
XMLAttributes.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12#include <string>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17namespace OpenMS::Internal
18{
28 class OPENMS_DLLAPI XMLAttributes
29 {
30 public:
31 using Values = std::vector<std::pair<std::u16string, std::u16string>>;
32
40 class OPENMS_DLLAPI NativeStore
41 {
42 public:
44 void clear() noexcept { count_ = 0; }
46 void add(std::string_view prefix, std::string_view local, std::string_view value);
48 Size size() const noexcept { return count_; }
49
50 private:
51 friend class XMLAttributes;
52 struct Entry
53 {
54 std::string_view local;
55 std::string_view value;
56 std::string joined;
57 bool prefixed = false;
58 mutable std::u16string value16;
59 mutable bool converted = false;
60
62 std::string_view name() const noexcept { return prefixed ? std::string_view(joined) : local; }
63 };
64 std::vector<Entry> entries_;
65 Size count_ = 0;
66 };
67
68 explicit XMLAttributes(const Values& attributes) noexcept : attributes_(&attributes) {}
69 explicit XMLAttributes(const NativeStore& attributes) noexcept : native_(&attributes) {}
70
71 XMLAttributes(Values&&) = delete;
72 XMLAttributes(const Values&&) = delete;
74 XMLAttributes(const NativeStore&&) = delete;
75
77 Size size() const noexcept;
78
80 const char16_t* value(const char16_t* qname) const;
81
83 const char16_t* value(const char* qname) const;
84
86 SignedSize index(const char16_t* qname) const;
87
89 const char16_t* valueByIndex(Size i) const;
90
92 bool valueUTF8(const char16_t* qname, std::string& value) const;
93
95 bool valueUTF8(const char* qname, std::string& value) const;
96
97 private:
99 SignedSize indexUTF8_(const char* qname) const;
101 void valueUTF8ByIndex_(Size i, std::string& value) const;
102
103 const Values* attributes_ = nullptr;
104 const NativeStore* native_ = nullptr;
105 };
106}
Reusable UTF-8 attribute storage of an XML parser adapter.
Definition XMLAttributes.h:41
void clear() noexcept
Forgets the attributes of the previous element (keeps capacity).
Definition XMLAttributes.h:44
std::vector< Entry > entries_
Definition XMLAttributes.h:64
void add(std::string_view prefix, std::string_view local, std::string_view value)
Appends an attribute; prefix may be empty. The qualified name is "prefix:local".
Size size() const noexcept
Number of attributes.
Definition XMLAttributes.h:48
Non-owning attribute view for an XML start-element callback.
Definition XMLAttributes.h:29
std::vector< std::pair< std::u16string, std::u16string > > Values
Definition XMLAttributes.h:31
Size size() const noexcept
Number of attributes.
XMLAttributes(Values &&)=delete
XMLAttributes(const Values &&)=delete
XMLAttributes(const NativeStore &attributes) noexcept
Definition XMLAttributes.h:69
XMLAttributes(const Values &attributes) noexcept
Definition XMLAttributes.h:68
XMLAttributes(const NativeStore &&)=delete
XMLAttributes(NativeStore &&)=delete
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Initialization procedures.
Definition BayesianProteinInferenceAlgorithm.h:26
STL namespace.
std::u16string value16
UTF-16 value, valid if converted.
Definition XMLAttributes.h:58
std::string_view name() const noexcept
UTF-8 qualified name (a view into joined is only taken here, as entries move when the store grows)
Definition XMLAttributes.h:62
std::string_view local
UTF-8 name if unprefixed.
Definition XMLAttributes.h:54
std::string_view value
UTF-8 value.
Definition XMLAttributes.h:55
std::string joined
UTF-8 "prefix:local" if prefixed.
Definition XMLAttributes.h:56