Skip to content

Commit

Permalink
String utility functions (#3366)
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
Mihai Budiu authored May 27, 2022
1 parent 6614b99 commit ab68233
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion frontends/p4/methodInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MethodInstance::resolve(const IR::MethodCallExpression* mce, DeclarationLookup*
auto mt = typeMap ? typeMap->getType(mce->method) : nullptr;
if (mt == nullptr && useExpressionType)
mt = mce->method->type;
CHECK_NULL(mt);
BUG_CHECK(mt, "%1%: unknown type", mce->method);
BUG_CHECK(mt->is<IR::Type_MethodBase>(), "%1%: expected a MethodBase type", mt);
auto originalType = mt->to<IR::Type_MethodBase>();
auto actualType = originalType;
Expand Down
11 changes: 10 additions & 1 deletion ir/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ limitations under the License.
#include "ir/json_loader.h"

#include "node.h"
// #include <signal.h>

void IR::Node::traceVisit(const char* visitor) const
{ LOG3("Visiting " << visitor << " " << id << ":" << node_type_name()); }

void IR::Node::traceCreation() const { LOG5("Created node " << id); }
void IR::Node::traceCreation() const {
/*
You can use this to trigger a breakpoint in the debugger when a
specific node is created
if (id == 279493)
raise(SIGINT);
*/
LOG5("Created node " << id);
}

int IR::Node::currentId = 0;

Expand Down
12 changes: 10 additions & 2 deletions lib/cstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ cstring cstring::replace(cstring search, cstring replace) const {
return cstring(s_str);
}

cstring cstring::indent(size_t amount) const {
std::string spaces = "";
for (size_t i = 0; i < amount; i++)
spaces += " ";
cstring spc = cstring("\n") + spaces;
return cstring(spaces) + replace("\n", spc);
}

// See https://stackoverflow.com/a/33799784/4538702
cstring cstring::escapeJson() const {
std::ostringstream o;
Expand All @@ -258,14 +266,14 @@ cstring cstring::escapeJson() const {
return cstring(o.str());
}

cstring cstring::toUpper() {
cstring cstring::toUpper() const {
std::string st = str;
std::transform(st.begin(), st.end(), st.begin(), ::toupper);
cstring ret = cstring::to_cstring(st);
return ret;
}

cstring cstring::capitalize() {
cstring cstring::capitalize() const {
std::string st = str;
st[0] = ::toupper(st[0]);
cstring ret = cstring::to_cstring(st);
Expand Down
10 changes: 6 additions & 4 deletions lib/cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,12 @@ class cstring {
/// to the total number of interned strings.
static size_t cache_size(size_t &count);

// convert the cstring to upper case
cstring toUpper();
// capitalize the first symbol
cstring capitalize();
/// convert the cstring to upper case
cstring toUpper() const;
/// capitalize the first symbol
cstring capitalize() const;
/// Append this many spaces after each newline (and before the first string).
cstring indent(size_t amount) const;
};

inline bool operator==(const char *a, cstring b) { return b == a; }
Expand Down

0 comments on commit ab68233

Please sign in to comment.