Skip to content

Commit

Permalink
Units use std::string
Browse files Browse the repository at this point in the history
Signed-off-by: James Cherry <[email protected]>
  • Loading branch information
jjcherry56 committed Nov 14, 2024
1 parent c831ff8 commit 3f52687
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
13 changes: 8 additions & 5 deletions include/sta/Units.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

#pragma once

#include <string>

namespace sta {

using std::string;

class Unit
{
public:
Unit(const char *suffix);
~Unit();
Unit(float scale,
const char *suffix,
int digits);
Expand All @@ -34,9 +37,9 @@ public:
float scale() const { return scale_; }
void setScale(float scale);
const char *scaleAbbreviation() const;
const char *suffix() const { return suffix_; }
const char *suffix() const { return suffix_.c_str(); }
// scale abbreviation + suffix
const char *scaledSuffix() const { return scaled_suffix_; }
const char *scaledSuffix() const { return scaled_suffix_.c_str(); }
void setSuffix(const char *suffix);
int digits() const { return digits_; }
void setDigits(int digits);
Expand All @@ -51,8 +54,8 @@ private:
void setScaledSuffix();

float scale_; // multiplier from user units to internal units
const char *suffix_; // print suffix
const char *scaled_suffix_;
string suffix_; // print suffix
string scaled_suffix_;
int digits_; // print digits (after decimal pt)
};

Expand Down
24 changes: 6 additions & 18 deletions liberty/Units.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ using std::abs;

Unit::Unit(const char *suffix) :
scale_(1.0),
suffix_(stringCopy(suffix)),
scaled_suffix_(nullptr),
suffix_(suffix),
digits_(3)
{
setScaledSuffix();
Expand All @@ -40,8 +39,7 @@ Unit::Unit(float scale,
const char *suffix,
int digits) :
scale_(scale),
suffix_(stringCopy(suffix)),
scaled_suffix_(nullptr),
suffix_(suffix),
digits_(digits)
{
setScaledSuffix();
Expand All @@ -50,24 +48,15 @@ Unit::Unit(float scale,
void
Unit::setScaledSuffix()
{
stringDelete(scaled_suffix_);
scaled_suffix_ = stringPrint("%s%s", scaleAbbreviation(), suffix_);
}

Unit::~Unit()
{
stringDelete(suffix_);
stringDelete(scaled_suffix_);
scaled_suffix_ = scaleAbbreviation() + suffix_;
}

void
Unit::operator=(const Unit &unit)
{
scale_ = unit.scale_;
stringDelete(suffix_);
suffix_ = stringCopy(unit.suffix_);
stringDelete(scaled_suffix_);
scaled_suffix_ = stringCopy(unit.scaled_suffix_);
suffix_ = unit.suffix_;
scaled_suffix_ = unit.scaled_suffix_;
digits_ = unit.digits_;
}

Expand Down Expand Up @@ -116,8 +105,7 @@ Unit::scaleAbbreviation() const
void
Unit::setSuffix(const char *suffix)
{
stringDelete(suffix_);
suffix_ = stringCopy(suffix);
suffix_ = suffix;
setScaledSuffix();
}

Expand Down

0 comments on commit 3f52687

Please sign in to comment.