Skip to content

Commit 9c6d149

Browse files
replace some string& with string_view
1 parent 3a69da0 commit 9c6d149

File tree

7 files changed

+38
-36
lines changed

7 files changed

+38
-36
lines changed

libs/libvtrutil/src/vtr_util.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static int cont; /* line continued? (used by strtok)*/
2626
*
2727
* The split strings (excluding the delimiters) are returned
2828
*/
29-
std::vector<std::string> split(const char* text, const std::string& delims) {
29+
std::vector<std::string> split(const char* text, std::string_view delims) {
3030
if (text) {
3131
std::string text_str(text);
3232
return split(text_str, delims);
@@ -39,13 +39,13 @@ std::vector<std::string> split(const char* text, const std::string& delims) {
3939
*
4040
* The split strings (excluding the delimiters) are returned
4141
*/
42-
std::vector<std::string> split(const std::string& text, const std::string& delims) {
42+
std::vector<std::string> split(std::string_view text, std::string_view delims) {
4343
std::vector<std::string> tokens;
4444

4545
std::string curr_tok;
4646
for (char c : text) {
4747
if (delims.find(c) != std::string::npos) {
48-
//Delimeter character
48+
//Delimiter character
4949
if (!curr_tok.empty()) {
5050
//At the end of the token
5151

@@ -58,7 +58,7 @@ std::vector<std::string> split(const std::string& text, const std::string& delim
5858
//Pass
5959
}
6060
} else {
61-
//Non-delimeter append to token
61+
//Non-delimiter append to token
6262
curr_tok += c;
6363
}
6464
}

libs/libvtrutil/src/vtr_util.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <algorithm>
55
#include <vector>
66
#include <string>
7+
#include <string_view>
78
#include <cstdarg>
89
#include <array>
910

@@ -14,8 +15,8 @@ namespace vtr {
1415
*
1516
* The split strings (excluding the delimiters) are returned
1617
*/
17-
std::vector<std::string> split(const char* text, const std::string& delims = " \t\n");
18-
std::vector<std::string> split(const std::string& text, const std::string& delims = " \t\n");
18+
std::vector<std::string> split(const char* text, std::string_view string_view = " \t\n");
19+
std::vector<std::string> split(std::string_view text, std::string_view delims = " \t\n");
1920

2021
///@brief Returns 'input' with the first instance of 'search' replaced with 'replace'
2122
std::string replace_first(const std::string& input, const std::string& search, const std::string& replace);

utils/fasm/src/fasm.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ static LogicVec lut_outputs(const t_pb* atom_pb, size_t num_inputs, const t_pb_r
413413
return lut.table();
414414
}
415415

416-
const t_metadata_dict *FasmWriterVisitor::get_fasm_type(const t_pb_graph_node* pb_graph_node, const std::string& target_type) const {
416+
const t_metadata_dict *FasmWriterVisitor::get_fasm_type(const t_pb_graph_node* pb_graph_node, std::string_view target_type) const {
417417
if(pb_graph_node == nullptr) {
418418
return nullptr;
419419
}
@@ -659,7 +659,7 @@ void FasmWriterVisitor::find_clb_prefix(const t_pb_graph_node *node,
659659
}
660660
}
661661

662-
void FasmWriterVisitor::output_fasm_mux(const std::string& fasm_mux_str,
662+
void FasmWriterVisitor::output_fasm_mux(std::string_view fasm_mux_str,
663663
t_interconnect *interconnect,
664664
const t_pb_graph_pin *mux_input_pin) {
665665
auto *pb_name = mux_input_pin->parent_node->pb_type->name;
@@ -741,15 +741,15 @@ void FasmWriterVisitor::output_fasm_mux(const std::string& fasm_mux_str,
741741

742742
vpr_throw(VPR_ERROR_OTHER, __FILE__, __LINE__,
743743
"fasm_mux %s[%d].%s[%d] found no matches in:\n%s\n",
744-
pb_name, pb_index, port_name, pin_index, fasm_mux_str.c_str());
744+
pb_name, pb_index, port_name, pin_index, fasm_mux_str.data());
745745
}
746746

747747
void FasmWriterVisitor::output_fasm_features(const std::string& features) const {
748748
output_fasm_features(features, clb_prefix_, blk_prefix_);
749749
}
750750

751-
void FasmWriterVisitor::output_fasm_features(const std::string& features, const std::string& clb_prefix, const std::string& blk_prefix) const {
752-
std::stringstream os(features);
751+
void FasmWriterVisitor::output_fasm_features(const std::string& features, std::string_view clb_prefix, std::string_view blk_prefix) const {
752+
std::istringstream os(features);
753753

754754
while(os) {
755755
std::string feature;

utils/fasm/src/fasm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <set>
1616
#include <sstream>
1717
#include <string>
18+
#include <string_view>
1819
#include <unordered_map>
1920
#include <vector>
2021

@@ -67,11 +68,11 @@ class FasmWriterVisitor : public NetlistVisitor {
6768

6869
private:
6970
void output_fasm_features(const std::string& features) const;
70-
void output_fasm_features(const std::string& features, const std::string& clb_prefix, const std::string& blk_prefix) const;
71+
void output_fasm_features(const std::string& features, std::string_view clb_prefix, std::string_view blk_prefix) const;
7172
void check_features(const t_metadata_dict *meta) const;
7273
void check_interconnect(const t_pb_routes &pb_route, int inode);
7374
void check_for_lut(const t_pb* atom);
74-
void output_fasm_mux(const std::string& fasm_mux, t_interconnect *interconnect, const t_pb_graph_pin *mux_input_pin);
75+
void output_fasm_mux(std::string_view fasm_mux, t_interconnect *interconnect, const t_pb_graph_pin *mux_input_pin);
7576
void walk_routing();
7677
void walk_route_tree(const RRGraphBuilder& rr_graph_builder, const RouteTreeNode& root);
7778
std::string build_clb_prefix(const t_pb *pb, const t_pb_graph_node* pb_graph_node, bool* is_parent_pb_null) const;
@@ -83,7 +84,7 @@ class FasmWriterVisitor : public NetlistVisitor {
8384
bool *have_prefix, std::string *clb_prefix) const;
8485
std::string handle_fasm_prefix(const t_metadata_dict *meta,
8586
const t_pb_graph_node *pb_graph_node, const t_pb_type *pb_type) const;
86-
const t_metadata_dict *get_fasm_type(const t_pb_graph_node* pb_graph_node, const std::string& target_type) const;
87+
const t_metadata_dict *get_fasm_type(const t_pb_graph_node* pb_graph_node, std::string_view target_type) const;
8788

8889
vtr::string_internment *strings_;
8990
std::ostream& os_;

utils/fasm/src/fasm_utils.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace fasm {
77

8-
void parse_name_with_optional_index(const std::string in, std::string *name, int *index) {
8+
void parse_name_with_optional_index(std::string_view in, std::string *name, int *index) {
99
auto in_parts = vtr::split(in, "[]");
1010

1111
if(in_parts.size() == 1) {
@@ -16,13 +16,13 @@ void parse_name_with_optional_index(const std::string in, std::string *name, int
1616
*index = vtr::atoi(in_parts[1]);
1717
} else {
1818
vpr_throw(VPR_ERROR_OTHER, __FILE__, __LINE__,
19-
"Cannot parse %s.", in.c_str());
19+
"Cannot parse %s.", in.data());
2020
}
2121
}
2222

2323
std::vector<std::string> split_fasm_entry(std::string entry,
24-
std::string delims,
25-
std::string ignore) {
24+
std::string_view delims,
25+
std::string_view ignore) {
2626
for (size_t ii=0; ii<entry.length(); ii++) {
2727
while (ignore.find(entry[ii]) != std::string::npos) {
2828
entry.erase(ii, 1);
@@ -32,7 +32,7 @@ std::vector<std::string> split_fasm_entry(std::string entry,
3232
return vtr::split(entry, delims);
3333
}
3434

35-
std::vector<std::string> find_tags_in_feature (const std::string& a_String) {
35+
std::vector<std::string> find_tags_in_feature (std::string_view a_String) {
3636
const std::regex regex ("(\\{[a-zA-Z0-9_]+\\})");
3737

3838
std::vector<std::string> tags;
@@ -51,17 +51,17 @@ std::vector<std::string> find_tags_in_feature (const std::string& a_String) {
5151
return tags;
5252
}
5353

54-
std::string substitute_tags (const std::string& a_Feature, const std::map<const std::string, std::string>& a_Tags) {
54+
std::string substitute_tags (std::string_view a_Feature, const std::map<const std::string, std::string>& a_Tags) {
5555

5656
// First list tags that are given in the feature string
5757
auto tags = find_tags_in_feature(a_Feature);
5858
if (tags.empty()) {
59-
return a_Feature;
59+
return std::string{a_Feature};
6060
}
6161

6262
// Check if those tags are defined, If not then throw an error
6363
bool have_errors = false;
64-
for (auto tag: tags) {
64+
for (const auto& tag: tags) {
6565
if (a_Tags.count(tag) == 0) {
6666
vtr::printf_error(__FILE__, __LINE__, "fasm placeholder '%s' not defined!", tag.c_str());
6767
have_errors = true;
@@ -71,13 +71,13 @@ std::string substitute_tags (const std::string& a_Feature, const std::map<const
7171
if (have_errors) {
7272
vpr_throw(VPR_ERROR_OTHER, __FILE__, __LINE__,
7373
"fasm feature '%s' contains placeholders that are not defined for the tile that it is used in!",
74-
a_Feature.c_str()
74+
a_Feature.data()
7575
);
7676
}
7777

7878
// Substitute tags
7979
std::string feature(a_Feature);
80-
for (auto tag: tags) {
80+
for (const auto& tag: tags) {
8181
std::regex regex("\\{" + tag + "\\}");
8282
feature = std::regex_replace(feature, regex, a_Tags.at(tag));
8383
}

utils/fasm/src/fasm_utils.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define UTILS_FASM_FASM_UTILS_H_
33

44
#include <string>
5+
#include <string_view>
56
#include <vector>
67
#include <map>
78

@@ -13,27 +14,27 @@ namespace fasm {
1314
// in="A[5]" parts to *name="A", *index=5
1415
//
1516
// Throws vpr exception if parsing fails.
16-
void parse_name_with_optional_index(const std::string in, std::string *name, int *index);
17+
void parse_name_with_optional_index(std::string_view in, std::string *name, int *index);
1718

1819
// Split FASM entry into parts.
1920
//
2021
// delims - Characters to split on.
2122
// ignore - Characters to ignore.
2223
std::vector<std::string> split_fasm_entry(std::string entry,
23-
std::string delims,
24-
std::string ignore);
24+
std::string_view delims,
25+
std::string_view ignore);
2526

2627
// Searches for tags in given string, returns their names in a vector.
27-
std::vector<std::string> find_tags_in_feature (const std::string& a_String);
28+
std::vector<std::string> find_tags_in_feature(std::string_view a_String);
2829

2930
// Substitutes tags found in a string with their values provided by the map.
30-
// Thorws an error if a tag is found in the string and its value is not present
31+
// Throws an error if a tag is found in the string and its value is not present
3132
// in the map.
3233
//
3334
// a_Feature - Fasm feature string (or any other string)
3435
// a_Tags - Map with tags and their values
35-
std::string substitute_tags (const std::string& a_Feature,
36-
const std::map<const std::string, std::string>& a_Tags);
36+
std::string substitute_tags(std::string_view a_Feature,
37+
const std::map<const std::string, std::string>& a_Tags);
3738

3839
} // namespace fasm
3940

vpr/src/base/vpr_api.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ void vpr_init_with_options(const t_options* options, t_vpr_setup* vpr_setup, t_a
251251
* Initialize the functions names for which VPR_ERRORs
252252
* are demoted to VTR_LOG_WARNs
253253
*/
254-
for (const std::string& func_name : vtr::split(options->disable_errors, std::string(":"))) {
254+
for (const std::string& func_name : vtr::split(options->disable_errors.value(), ":")) {
255255
map_error_activation_status(func_name);
256256
}
257257

258258
/*
259259
* Initialize the functions names for which
260260
* warnings are being suppressed
261261
*/
262-
std::vector<std::string> split_warning_option = vtr::split(options->suppress_warnings, std::string(","));
262+
std::vector<std::string> split_warning_option = vtr::split(options->suppress_warnings.value(), ",");
263263
std::string warn_log_file;
264264
std::string warn_functions;
265265
// If no log file name is provided, the specified warning
@@ -507,7 +507,7 @@ void vpr_create_device_grid(const t_vpr_setup& vpr_setup, const t_arch& Arch) {
507507
if (!device_ctx.grid.limiting_resources().empty()) {
508508
std::vector<std::string> limiting_block_names;
509509
for (auto blk_type : device_ctx.grid.limiting_resources()) {
510-
limiting_block_names.push_back(blk_type->name);
510+
limiting_block_names.emplace_back(blk_type->name);
511511
}
512512
VTR_LOG("FPGA size limited by block type(s): %s\n", vtr::join(limiting_block_names, " ").c_str());
513513
VTR_LOG("\n");
@@ -565,7 +565,6 @@ void vpr_setup_noc_routing_algorithm(const std::string& noc_routing_algorithm_na
565565
auto& noc_ctx = g_vpr_ctx.mutable_noc();
566566

567567
noc_ctx.noc_flows_router = NocRoutingAlgorithmCreator::create_routing_algorithm(noc_routing_algorithm_name);
568-
return;
569568
}
570569

571570
bool vpr_pack_flow(t_vpr_setup& vpr_setup, const t_arch& arch) {
@@ -598,7 +597,7 @@ bool vpr_pack_flow(t_vpr_setup& vpr_setup, const t_arch& arch) {
598597
check_netlist(packer_opts.pack_verbosity);
599598

600599
/* Output the netlist stats to console and optionally to file. */
601-
writeClusteredNetlistStats(vpr_setup.FileNameOpts.write_block_usage.c_str());
600+
writeClusteredNetlistStats(vpr_setup.FileNameOpts.write_block_usage);
602601

603602
// print the total number of used physical blocks for each
604603
// physical block type after finishing the packing stage

0 commit comments

Comments
 (0)