19160b844SJim Walker/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
29160b844SJim Walker/*
39160b844SJim Walker *     Copyright 2017 Couchbase, Inc
49160b844SJim Walker *
59160b844SJim Walker *   Licensed under the Apache License, Version 2.0 (the "License");
69160b844SJim Walker *   you may not use this file except in compliance with the License.
79160b844SJim Walker *   You may obtain a copy of the License at
89160b844SJim Walker *
99160b844SJim Walker *       http://www.apache.org/licenses/LICENSE-2.0
109160b844SJim Walker *
119160b844SJim Walker *   Unless required by applicable law or agreed to in writing, software
129160b844SJim Walker *   distributed under the License is distributed on an "AS IS" BASIS,
139160b844SJim Walker *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149160b844SJim Walker *   See the License for the specific language governing permissions and
159160b844SJim Walker *   limitations under the License.
169160b844SJim Walker */
179160b844SJim Walker
189160b844SJim Walker#include "collections/collections_types.h"
199160b844SJim Walker
20cd98cc03SJim Walker#include <cctype>
21cd98cc03SJim Walker#include <cstring>
2230aab6b7SJim Walker#include <iostream>
2330aab6b7SJim Walker
249160b844SJim Walkernamespace Collections {
259160b844SJim Walker
26cd98cc03SJim Walkeruid_t makeUid(const char* uid) {
27cd98cc03SJim Walker    if (std::strlen(uid) == 0 || std::strlen(uid) > 16) {
28cd98cc03SJim Walker        throw std::invalid_argument(
29cd98cc03SJim Walker                "Collections::convertUid uid must be > 0 and <= 16 characters: "
30cd98cc03SJim Walker                "strlen(uid):" +
31cd98cc03SJim Walker                std::to_string(std::strlen(uid)));
32cd98cc03SJim Walker    }
33cd98cc03SJim Walker
34cd98cc03SJim Walker    // verify that the input characters satisfy isxdigit
35cd98cc03SJim Walker    for (size_t ii = 0; ii < std::strlen(uid); ii++) {
36cd98cc03SJim Walker        if (uid[ii] == 0) {
37cd98cc03SJim Walker            break;
38cd98cc03SJim Walker        } else if (!std::isxdigit(uid[ii])) {
39cd98cc03SJim Walker            throw std::invalid_argument("Collections::convertUid: uid:" +
40cd98cc03SJim Walker                                        std::string(uid) + ", index:" +
41cd98cc03SJim Walker                                        std::to_string(ii) + " fails isxdigit");
42cd98cc03SJim Walker        }
43cd98cc03SJim Walker    }
44cd98cc03SJim Walker
45cd98cc03SJim Walker    return std::strtoull(uid, nullptr, 16);
46cd98cc03SJim Walker}
47cd98cc03SJim Walker
4830aab6b7SJim Walkerstd::string to_string(const Identifier& identifier) {
499160b844SJim Walker    return cb::to_string(identifier.getName()) + ":" +
509160b844SJim Walker           std::to_string(identifier.getUid());
519160b844SJim Walker}
529160b844SJim Walker
5330aab6b7SJim Walkerstd::ostream& operator<<(std::ostream& os, const Identifier& identifier) {
5430aab6b7SJim Walker    os << to_string(identifier);
5530aab6b7SJim Walker    return os;
5630aab6b7SJim Walker}
5730aab6b7SJim Walker
589160b844SJim Walker} // end namespace Collections