xref: /freebsd/usr.sbin/pmc/util.cc (revision 93da997ef759061670c1575eec92f922a645a5fd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2026, Netflix, Inc.
5  *
6  * This software was developed by Ali Mashtizadeh under the sponsorship from
7  * Netflix, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <string>
33 #include <unordered_set>
34 
35 #include "util.hh"
36 
37 std::string
basename(const std::string & path)38 basename(const std::string &path)
39 {
40 	size_t s;
41 
42 	s = path.rfind("/");
43 	if (s == std::string::npos)
44 		return (path);
45 	else
46 		return (path.substr(s + 1));
47 }
48 
49 void
split_and_insert(std::unordered_set<int> * set,const std::string & str)50 split_and_insert(std::unordered_set<int> *set, const std::string &str)
51 {
52 	size_t pos = 0;
53 
54 	while (pos < str.length()) {
55 		size_t end = str.find(",", pos);
56 		if (end == str.npos) {
57 			set->insert(std::stoi(str.substr(pos)));
58 			break;
59 		}
60 
61 		set->insert(std::stoi(str.substr(pos, end - pos)));
62 		pos = end + 1;
63 	}
64 }
65 
66 void
split_and_insert(std::unordered_set<std::string> * set,const std::string & str)67 split_and_insert(std::unordered_set<std::string> *set, const std::string &str)
68 {
69 	size_t pos = 0;
70 
71 	while (pos < str.length()) {
72 		size_t end = str.find(",", pos);
73 		if (end == str.npos) {
74 			set->insert(str.substr(pos));
75 			break;
76 		}
77 
78 		set->insert(str.substr(pos, end - pos));
79 		pos = end + 1;
80 	}
81 }
82 
83