aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/geo/util.cpp
blob: e8d0fc378e65ac6a2fbf663f7e9acb01e6343efa (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
32
33
34
#include "util.h"

#include <math.h>
#include <util/generic/cast.h>
#include <util/generic/string.h>
#include <util/string/cast.h>
#include <utility>

namespace NGeo {
    bool TryPairFromString(std::pair<double, double>& res, TStringBuf inputStr, TStringBuf delimiter) {
        TStringBuf lhsStr;
        TStringBuf rhsStr;

        double lhs = NAN;
        double rhs = NAN;
        if (
            !inputStr.TrySplit(delimiter, lhsStr, rhsStr) ||
            !TryFromString<double>(lhsStr, lhs) ||
            !TryFromString<double>(rhsStr, rhs)) {
            return false;
        }

        res = {lhs, rhs};
        return true;
    }

    std::pair<double, double> PairFromString(TStringBuf inputStr, TStringBuf delimiter) {
        std::pair<double, double> res;
        if (!TryPairFromString(res, inputStr, delimiter)) {
            ythrow TBadCastException() << "Wrong point string: " << inputStr;
        }
        return res;
    }
} // namespace NGeo