HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

C++ Wide string utility class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classstringwideutility

Problem

I created a class to add functionality to c++ std::string (std::basic_string actually), let's call it String.

I would like to know, what changes to do to String to boost it performance. Maybe having an internal std::basic_string member is not the best idea and it would be better to implement my own (that maybe would be re-inventing the wheel) or maybe the class itself is just not necessary and would be better just to write down functions under a namespace.

That being said, here is the code:

String.h

#pragma once

#include 

template 
class String {
public:
  String();
  String(const CharType*);
  String(std::basic_string);
  ~String();

  std::size_t length();
  std::size_t size();
  std::size_t bytesize();

  std::string to_bytes();

  static std::basic_string from_bytes(const char*);
  static std::basic_string from_bytes(std::string);

  std::basic_string std;
};


String.cpp

``
#include "String.h"

#include
#include

#include
#include

// Default constructor with no parameters
template
String::String() { }

// Constructor for:
// String variable = "Hi there";
// String variable = u"Hi there";
template
String::String(const CharType* str): std(str) { }

// Normal Constructor
// String variable("Hi there");
// String variable(u"Hi there");
template
String::String(std::basic_string str): std(str) { }

// Destructor
template
String::~String() { }

// Get the length of a string, it's not necessary to calculate it
// since std::string already provides this functionality
template
std::size_t String::length() {
return std.length();
}

// Now, in a string of
char` the length would be different of expected
// as actually it counts special characters by its number of bytesize
// with this functionality it counts those characters as one
// Demonstration
// std::char_traits::length("Ni hao 你好")
// ^ 13 since each chinese character counts as 3
//
// String("Ni hao 你好").length();
// ^ 9 as expected
//
// Def

Solution

Shouldn't these declarations without definitions be sufficient?

String() = default;
~String() = default;


I would personally skip such not-strictly-necessary declarations completely but that is probably just a subjective opinion.

Would you like to enable using String as a base class?
If so you might probably need to consider having virtual destructor.

Some methods could be const:

std::size_t length() const;
std::size_t size() const;
std::size_t bytesize() const;
std::string to_bytes() const;


I guess you know that calling std::string(const char*) ctor with nullptr is undefined. It might be tricky to spot it behind your interface:

std::basic_string String::from_bytes(const char* str) {
    return str;
}

Code Snippets

String() = default;
~String() = default;
std::size_t length() const;
std::size_t size() const;
std::size_t bytesize() const;
std::string to_bytes() const;
std::basic_string<char> String<char>::from_bytes(const char* str) {
    return str;
}

Context

StackExchange Code Review Q#131531, answer score: 2

Revisions (0)

No revisions yet.