summaryrefslogtreecommitdiffstats
path: root/library/cpp/geo/size.cpp
blob: f1bd8ab76309e56998b3dc19a2abbc672d2f7bdf (plain) (blame)
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
#include "size.h"

#include "util.h"

namespace NGeo {
    const double TSize::BadWidth = -1.;
    const double TSize::BadHeight = -1.;

    namespace {
        bool IsNonNegativeSize(double width, double height) {
            return width >= 0. && height >= 0.;
        }
    } // namespace

    TSize TSize::Parse(TStringBuf s, TStringBuf delimiter) {
        const auto& [width, height] = PairFromString(s, delimiter);
        Y_ENSURE_EX(IsNonNegativeSize(width, height), TBadCastException() << "Negative window size");
        return {width, height};
    }

    TMaybe<TSize> TSize::TryParse(TStringBuf s, TStringBuf delimiter) {
        std::pair<double, double> lonLat;
        if (!TryPairFromString(lonLat, s, delimiter)) {
            return {};
        }
        if (!IsNonNegativeSize(lonLat.first, lonLat.second)) {
            return {};
        }
        return TSize{lonLat.first, lonLat.second};
    }
} // namespace NGeo