diff options
author | Alexander Smirnov <[email protected]> | 2025-02-04 00:51:36 +0000 |
---|---|---|
committer | Alexander Smirnov <[email protected]> | 2025-02-04 00:51:36 +0000 |
commit | a460fd487767efcedc9cdcf03a0e9355a8789e28 (patch) | |
tree | 4ba490df64d64728eced2d35d37470f40327fce8 /library/cpp | |
parent | f2b8dd76d7c8ae823caaa4b9438b55e910cbc8d3 (diff) | |
parent | 4da950958ca455803862272008bd5a2268831fb2 (diff) |
Merge branch 'rightlib' into merge-libs-250204-0050
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/xml/document/node-attr.h | 12 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 18 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/yt/compact_containers/compact_flat_map.h | 2 | ||||
-rw-r--r-- | library/cpp/yt/string/format-inl.h | 5 | ||||
-rw-r--r-- | library/cpp/yt/string/unittests/format_ut.cpp | 1 |
6 files changed, 59 insertions, 1 deletions
diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 1378ffdfbff..6f5445d4c2d 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -45,6 +45,18 @@ namespace NXml { } template <class T> + TMaybe<T> TNode::TryAttr(TZtStringBuf name) const { + TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + if (!value) { + return Nothing(); + } + + T t; + AttrInternal(value, t, name); + return t; + } + + template <class T> T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6e..643ba664680 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -328,6 +328,14 @@ namespace NXml { /** * get node attribute * @param name: attribute name + * returns value if exists + */ + template <class T> + TMaybe<T> TryAttr(TZtStringBuf name) const; + + /** + * get node attribute + * @param name: attribute name * returns default value if attribute not found */ template <class T> @@ -600,6 +608,16 @@ namespace NXml { /** * get node attribute * @param name: attribute name + * returns value if exists + */ + template <class T> + TMaybe<T> TryAttr(TZtStringBuf name) const { + return ActualNode.TryAttr<T>(name); + } + + /** + * get node attribute + * @param name: attribute name * throws exception if attribute not found */ template <class T> diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index 9f537b75c4c..0ec1fc60838 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -1,5 +1,6 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/generic/map.h> +#include <util/generic/yexception.h> #include "xml-document.h" @@ -35,6 +36,27 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { NXml::TConstNode text = root.Node("text"); UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст"); } + Y_UNIT_TEST(GetAttributes) { + NXml::TDocument xml(R"(<?xml version="1.0"?> + <root> + <a><b len="15" correct="1">hello world</b></a> + <text>Некоторый текст</text> + </root> + )", + NXml::TDocument::String); + + NXml::TConstNode root = xml.Root(); + NXml::TConstNode b = root.Node("a/b"); + + UNIT_ASSERT_EXCEPTION_CONTAINS(b.Attr<int>("unknown attrib"), yexception, "@unknown attrib"); + + const auto unknownAttr = b.TryAttr<int>("unknown attrib"); + UNIT_ASSERT(unknownAttr.Empty()); + + const auto knownAttr = b.TryAttr<int>("len"); + UNIT_ASSERT(knownAttr.Defined()); + UNIT_ASSERT_EQUAL(knownAttr.GetRef(), 15); + } Y_UNIT_TEST(SerializeString) { NXml::TDocument xml("frob", NXml::TDocument::RootName); xml.Root().SetAttr("xyzzy", "Frobozz"); diff --git a/library/cpp/yt/compact_containers/compact_flat_map.h b/library/cpp/yt/compact_containers/compact_flat_map.h index d4e93668430..49c8628c46c 100644 --- a/library/cpp/yt/compact_containers/compact_flat_map.h +++ b/library/cpp/yt/compact_containers/compact_flat_map.h @@ -20,7 +20,7 @@ concept CComparisonAllowed = std::same_as<T, U> || CHasIsTransparentFlag<TCompar /////////////////////////////////////////////////////////////////////////////// -//! A flat map implementation over TCompactTValueector that tries to keep data inline. +//! A flat map implementation over TCompactVector that tries to keep data inline. /*! * Similarly to SmallSet, this is implemented via binary search over a sorted * vector. Unlike SmallSet, however, this one never falls back to std::map (or diff --git a/library/cpp/yt/string/format-inl.h b/library/cpp/yt/string/format-inl.h index 83d61986745..22da423f893 100644 --- a/library/cpp/yt/string/format-inl.h +++ b/library/cpp/yt/string/format-inl.h @@ -11,6 +11,7 @@ #include <library/cpp/yt/assert/assert.h> #include <library/cpp/yt/compact_containers/compact_vector.h> +#include <library/cpp/yt/compact_containers/compact_flat_map.h> #include <library/cpp/yt/containers/enum_indexed_array.h> @@ -163,6 +164,10 @@ template <class... Ts> constexpr bool CKnownKVRange<THashMap<Ts...>> = true; template <class... Ts> constexpr bool CKnownKVRange<THashMultiMap<Ts...>> = true; +template <class... Ts> +constexpr bool CKnownKVRange<TCompactFlatMap<Ts...>> = true; +template <class K, class V, size_t N> +constexpr bool CKnownKVRange<TCompactFlatMap<K, V, N>> = true; // TODO(arkady-e1ppa): Uncomment me when // https://github.com/llvm/llvm-project/issues/58534 is shipped. diff --git a/library/cpp/yt/string/unittests/format_ut.cpp b/library/cpp/yt/string/unittests/format_ut.cpp index f783efcf11a..e2e23c737c2 100644 --- a/library/cpp/yt/string/unittests/format_ut.cpp +++ b/library/cpp/yt/string/unittests/format_ut.cpp @@ -70,6 +70,7 @@ static_assert(CFormattable<std::multimap<int, int>>); static_assert(CFormattable<THashSet<int>>); static_assert(CFormattable<THashMap<int, int>>); static_assert(CFormattable<THashMultiSet<int>>); +static_assert(CFormattable<TCompactFlatMap<int, int, 2>>); static_assert(CFormattable<std::pair<int, int>>); static_assert(CFormattable<std::optional<int>>); static_assert(CFormattable<TDuration>); |