diff options
| author | AlexSm <[email protected]> | 2023-12-27 23:31:58 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-12-27 23:31:58 +0100 |
| commit | d67bfb4b4b7549081543e87a31bc6cb5c46ac973 (patch) | |
| tree | 8674f2f1570877cb653e7ddcff37ba00288de15a /library/cpp/ipmath/ipmath.cpp | |
| parent | 1f6bef05ed441c3aa2d565ac792b26cded704ac7 (diff) | |
Import libs 4 (#758)
Diffstat (limited to 'library/cpp/ipmath/ipmath.cpp')
| -rw-r--r-- | library/cpp/ipmath/ipmath.cpp | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/library/cpp/ipmath/ipmath.cpp b/library/cpp/ipmath/ipmath.cpp index 53f19dbb017..b2dffcfcfdb 100644 --- a/library/cpp/ipmath/ipmath.cpp +++ b/library/cpp/ipmath/ipmath.cpp @@ -139,7 +139,7 @@ TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder return *this; } -TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithPrefix(ui8 len) { +TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithPrefixImpl(ui8 len, bool checkLowerBound) { Y_ENSURE_EX(IsValid(Start_), TInvalidIpRangeException() << "Start value must be set before prefix"); const auto type = Start_.Type(); const auto maxLen = MaxPrefixLenForType(type); @@ -147,14 +147,25 @@ TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder << maxLen << ", but requested " << (ui32)len); const auto lowerBound = LowerBoundForPrefix(Start_, len); - Y_ENSURE_EX(Start_ == lowerBound, TInvalidIpRangeException() << "Cannot create IP range from start address " - << Start_ << " with prefix length " << (ui32)len); + if (checkLowerBound) { + Y_ENSURE_EX(Start_ == lowerBound, TInvalidIpRangeException() << "Cannot create IP range from start address " + << Start_ << " with prefix length " << (ui32)len); + } + Start_ = lowerBound; End_ = UpperBoundForPrefix(Start_, len); return *this; } +TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithPrefix(ui8 len) { + return WithPrefixImpl(len, true); +} + +TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithMaskedPrefix(ui8 len) { + return WithPrefixImpl(len, false); +} + void TIpAddressRange::Init(TIpv6Address from, TIpv6Address to) { Start_ = from; End_ = to; @@ -230,7 +241,7 @@ TIpAddressRange TIpAddressRange::FromCidrString(const TStringBuf str) { ythrow TInvalidIpRangeException() << "Cannot parse " << str << " as a CIDR string"; } -TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrString(const TStringBuf str) { +TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrStringImpl(const TStringBuf str, bool compact) { auto idx = str.rfind('/'); if (idx == TStringBuf::npos) { return Nothing(); @@ -246,8 +257,25 @@ TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrString(const TStringBuf str) return Nothing(); } - return TIpAddressRange::From(address) - .WithPrefix(prefixLen); + return compact ? + TIpAddressRange::From(address).WithMaskedPrefix(prefixLen) : + TIpAddressRange::From(address).WithPrefix(prefixLen); +} + +TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrString(const TStringBuf str) { + return TryFromCidrStringImpl(str, false); +} + +TIpAddressRange TIpAddressRange::FromCompactString(const TStringBuf str) { + if (auto result = TryFromCompactString(str)) { + return *result; + } + + ythrow TInvalidIpRangeException() << "Cannot parse " << str << " as a CIDR string"; +} + +TMaybe<TIpAddressRange> TIpAddressRange::TryFromCompactString(const TStringBuf str) { + return TryFromCidrStringImpl(str, true); } TIpAddressRange TIpAddressRange::FromRangeString(const TStringBuf str) { |
