CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Text/Parsers.h
Views: 1401
#pragma once12#include <string>3#include <sstream>4#include <cstdint>56#undef major7#undef minor89// Parses version strings of the format "Major.Minor.Sub" and lets you interact with them conveniently.10struct Version {11Version() : major(0), minor(0), sub(0) {}12Version(const std::string &str) {13if (!ParseVersionString(str)) {14major = -1;15minor = -1;16sub = -1;17}18}1920int major;21int minor;22int sub;2324bool IsValid() const {25return sub >= 0 && minor >= 0 && major >= 0;26}2728bool operator == (const Version &other) const {29return major == other.major && minor == other.minor && sub == other.sub;30}31bool operator != (const Version &other) const {32return !(*this == other);33}3435bool operator <(const Version &other) const {36if (major < other.major) return true;37if (major > other.major) return false;38if (minor < other.minor) return true;39if (minor > other.minor) return false;40if (sub < other.sub) return true;41if (sub > other.sub) return false;42return false;43}4445bool operator >=(const Version &other) const {46return !(*this < other);47}4849std::string ToString() const;50int ToInteger() const;51private:52bool ParseVersionString(std::string str);53};5455bool ParseMacAddress(const std::string &str, uint8_t macAddr[6]);5657bool TryParse(const std::string &str, bool *const output);58bool TryParse(const std::string &str, uint32_t *const output);59bool TryParse(const std::string &str, uint64_t *const output);6061template <typename N>62static bool TryParse(const std::string &str, N *const output) {63std::istringstream iss(str);6465N tmp = 0;66if (iss >> tmp) {67*output = tmp;68return true;69} else70return false;71}7273void NiceSizeFormat(uint64_t size, char *out, size_t bufSize);7475std::string NiceSizeFormat(uint64_t size);767778