-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathStringUtils.cpp
68 lines (58 loc) · 1.98 KB
/
StringUtils.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//===- StringUtils.cpp - Routines for manipulating swiftscan_string_ref_t -===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/DependencyScan/StringUtils.h"
#include <string>
#include <vector>
namespace swift {
namespace c_string_utils {
swiftscan_string_ref_t create_null() {
swiftscan_string_ref_t str;
str.data = nullptr;
str.length = 0;
return str;
}
swiftscan_string_ref_t create_clone(const char *string) {
if (!string)
return create_null();
if (string[0] == '\0')
return create_null();
swiftscan_string_ref_t str;
str.data = strdup(string);
str.length = strlen(string);
return str;
}
swiftscan_string_set_t *create_set(const std::vector<std::string> &strings) {
swiftscan_string_set_t *set = new swiftscan_string_set_t;
set->count = strings.size();
set->strings = new swiftscan_string_ref_t[set->count];
for (unsigned SI = 0, SE = set->count; SI < SE; ++SI)
set->strings[SI] = create_clone(strings[SI].c_str());
return set;
}
swiftscan_string_set_t *create_set(int count, const char **strings) {
swiftscan_string_set_t *set = new swiftscan_string_set_t;
set->count = count;
set->strings = new swiftscan_string_ref_t[set->count];
for (unsigned SI = 0, SE = set->count; SI < SE; ++SI)
set->strings[SI] = create_clone(strings[SI]);
return set;
}
swiftscan_string_set_t *create_empty_set() {
swiftscan_string_set_t *set = new swiftscan_string_set_t;
set->count = 0;
return set;
}
const char *get_C_string(swiftscan_string_ref_t string) {
return static_cast<const char *>(string.data);
}
} // namespace c_string_utils
} // namespace swift