1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#include "collections/collections_types.h"
19
20#include <cctype>
21#include <cstring>
22#include <iostream>
23
24namespace Collections {
25
26uid_t makeUid(const char* uid) {
27    if (std::strlen(uid) == 0 || std::strlen(uid) > 16) {
28        throw std::invalid_argument(
29                "Collections::convertUid uid must be > 0 and <= 16 characters: "
30                "strlen(uid):" +
31                std::to_string(std::strlen(uid)));
32    }
33
34    // verify that the input characters satisfy isxdigit
35    for (size_t ii = 0; ii < std::strlen(uid); ii++) {
36        if (uid[ii] == 0) {
37            break;
38        } else if (!std::isxdigit(uid[ii])) {
39            throw std::invalid_argument("Collections::convertUid: uid:" +
40                                        std::string(uid) + ", index:" +
41                                        std::to_string(ii) + " fails isxdigit");
42        }
43    }
44
45    return std::strtoull(uid, nullptr, 16);
46}
47
48std::string to_string(const Identifier& identifier) {
49    return cb::to_string(identifier.getName()) + ":" +
50           std::to_string(identifier.getUid());
51}
52
53std::ostream& operator<<(std::ostream& os, const Identifier& identifier) {
54    os << to_string(identifier);
55    return os;
56}
57
58} // end namespace Collections