Bit of code-modernization cleanup in BroString

- Convert single-bit bit fields into bools
- Use bool for a number of function arguments that were previously ints
- Use delegated constructors to reduce repetition in the other constructors
This commit is contained in:
Tim Wojtulewicz 2020-01-02 13:20:51 -07:00
parent a463c5763f
commit 9d38419e8a
2 changed files with 28 additions and 40 deletions

View file

@ -25,52 +25,40 @@ const int BroString::BRO_STRING_LITERAL;
// arg_final_NUL == 1; when str is a sequence of n bytes, make // arg_final_NUL == 1; when str is a sequence of n bytes, make
// arg_final_NUL == 0. // arg_final_NUL == 0.
BroString::BroString(int arg_final_NUL, byte_vec str, int arg_n) BroString::BroString(bool arg_final_NUL, byte_vec str, int arg_n)
{ {
b = str; b = str;
n = arg_n; n = arg_n;
final_NUL = arg_final_NUL; final_NUL = arg_final_NUL;
use_free_to_delete = 0; use_free_to_delete = false;
} }
BroString::BroString(const u_char* str, int arg_n, int add_NUL) BroString::BroString(const u_char* str, int arg_n, bool add_NUL) : BroString()
{ {
b = 0;
n = 0;
use_free_to_delete = 0;
Set(str, arg_n, add_NUL); Set(str, arg_n, add_NUL);
} }
BroString::BroString(const char* str) BroString::BroString(const char* str) : BroString()
{ {
b = 0;
n = 0;
use_free_to_delete = 0;
Set(str); Set(str);
} }
BroString::BroString(const string &str) BroString::BroString(const string &str) : BroString()
{ {
b = 0;
n = 0;
use_free_to_delete = 0;
Set(str); Set(str);
} }
BroString::BroString(const BroString& bs) BroString::BroString(const BroString& bs) : BroString()
{ {
b = 0;
n = 0;
use_free_to_delete = 0;
*this = bs; *this = bs;
} }
BroString::BroString() BroString::BroString()
{ {
b = 0; b = nullptr;
n = 0; n = 0;
final_NUL = 0; final_NUL = false;
use_free_to_delete = 0; use_free_to_delete = false;
} }
void BroString::Reset() void BroString::Reset()
@ -80,10 +68,10 @@ void BroString::Reset()
else else
delete [] b; delete [] b;
b = 0; b = nullptr;
n = 0; n = 0;
final_NUL = 0; final_NUL = false;
use_free_to_delete = 0; use_free_to_delete = false;
} }
const BroString& BroString::operator=(const BroString &bs) const BroString& BroString::operator=(const BroString &bs)
@ -95,8 +83,8 @@ const BroString& BroString::operator=(const BroString &bs)
memcpy(b, bs.b, n); memcpy(b, bs.b, n);
b[n] = '\0'; b[n] = '\0';
final_NUL = 1; final_NUL = true;
use_free_to_delete = 0; use_free_to_delete = false;
return *this; return *this;
} }
@ -122,7 +110,7 @@ void BroString::Adopt(byte_vec bytes, int len)
n = len - final_NUL; n = len - final_NUL;
} }
void BroString::Set(const u_char* str, int len, int add_NUL) void BroString::Set(const u_char* str, int len, bool add_NUL)
{ {
Reset(); Reset();
@ -134,7 +122,7 @@ void BroString::Set(const u_char* str, int len, int add_NUL)
if ( add_NUL ) if ( add_NUL )
b[n] = 0; b[n] = 0;
use_free_to_delete = 0; use_free_to_delete = false;
} }
void BroString::Set(const char* str) void BroString::Set(const char* str)
@ -144,8 +132,8 @@ void BroString::Set(const char* str)
n = strlen(str); n = strlen(str);
b = new u_char[n+1]; b = new u_char[n+1];
memcpy(b, str, n+1); memcpy(b, str, n+1);
final_NUL = 1; final_NUL = true;
use_free_to_delete = 0; use_free_to_delete = false;
} }
void BroString::Set(const string& str) void BroString::Set(const string& str)
@ -155,8 +143,8 @@ void BroString::Set(const string& str)
n = str.size(); n = str.size();
b = new u_char[n+1]; b = new u_char[n+1];
memcpy(b, str.c_str(), n+1); memcpy(b, str.c_str(), n+1);
final_NUL = 1; final_NUL = true;
use_free_to_delete = 0; use_free_to_delete = false;
} }
void BroString::Set(const BroString& str) void BroString::Set(const BroString& str)
@ -307,7 +295,7 @@ BroString::Vec* BroString::Split(const BroString::IdxVec& indices) const
{ {
unsigned int i; unsigned int i;
if ( indices.size() == 0 ) if ( indices.empty() )
return 0; return 0;
// Copy input, ensuring space for "0": // Copy input, ensuring space for "0":
@ -453,7 +441,7 @@ BroString* concatenate(std::vector<data_chunk_t>& v)
*b = '\0'; *b = '\0';
return new BroString(1, (byte_vec) data, len); return new BroString(true, (byte_vec) data, len);
} }
BroString* concatenate(BroString::CVec& v) BroString* concatenate(BroString::CVec& v)
@ -474,7 +462,7 @@ BroString* concatenate(BroString::CVec& v)
} }
*b = '\0'; *b = '\0';
return new BroString(1, (byte_vec) data, len); return new BroString(true, (byte_vec) data, len);
} }
BroString* concatenate(BroString::Vec& v) BroString* concatenate(BroString::Vec& v)

View file

@ -33,13 +33,13 @@ public:
typedef IdxVec::const_iterator IdxVecCIt; typedef IdxVec::const_iterator IdxVecCIt;
// Constructors creating internal copies of the data passed in. // Constructors creating internal copies of the data passed in.
BroString(const u_char* str, int arg_n, int add_NUL); BroString(const u_char* str, int arg_n, bool add_NUL);
explicit BroString(const char* str); explicit BroString(const char* str);
explicit BroString(const std::string& str); explicit BroString(const std::string& str);
BroString(const BroString& bs); BroString(const BroString& bs);
// Constructor that takes owernship of the vector passed in. // Constructor that takes owernship of the vector passed in.
BroString(int arg_final_NUL, byte_vec str, int arg_n); BroString(bool arg_final_NUL, byte_vec str, int arg_n);
BroString(); BroString();
~BroString() { Reset(); } ~BroString() { Reset(); }
@ -61,7 +61,7 @@ public:
// current contents, if any, and then set the string's // current contents, if any, and then set the string's
// contents to a copy of the string given by the arguments. // contents to a copy of the string given by the arguments.
// //
void Set(const u_char* str, int len, int add_NUL=1); void Set(const u_char* str, int len, bool add_NUL=true);
void Set(const char* str); void Set(const char* str);
void Set(const std::string& str); void Set(const std::string& str);
void Set(const BroString &str); void Set(const BroString &str);
@ -146,8 +146,8 @@ protected:
byte_vec b; byte_vec b;
int n; int n;
unsigned int final_NUL:1; // whether we have added a final NUL bool final_NUL; // whether we have added a final NUL
unsigned int use_free_to_delete:1; // free() vs. operator delete bool use_free_to_delete; // free() vs. operator delete
}; };
// A comparison class that sorts pointers to BroString's according to // A comparison class that sorts pointers to BroString's according to