Skip to content

vpiotr/char_view

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This is readme file for char_view - immutable string view class with compile-time processing with support for C++ string literals, std::strings and char buffers. Library is currently implemented as header-only solution.

License

Software is published under BSD license, see license.txt for details.

Project home

https://github.com/vpiotr/char_view

Purpose

  • for lightweight string literal handling in C++ way (with functions from std::string)
  • to support compile-time properties (length, hash) and search methods (find, contains, equals, etc)
  • to support strings in switch statement

Benefits

  • allows compile-time string function calculations:

      using namespace sbt;
      
      // check if string has prefix "std::"
      constexpr bool HasStdPrefix(const char_view &value) {
          return value.starts_with("std::"_cv);
      }
    
      // assert calculated in compile time
      bool TestPrefix() {
          constexpr char_view s1("std::list");
          Assert(HasStdPrefix(s1), "HasStdPrefix");
    
          return true;
      }
    
  • passing parameter as literal does not involve deep copying - has complexity = O(1)

  • allows using strings in switch statement:

      bool is_command_ok(const std::string &str) 
      {
          switch (char_view(str.c_str()).hash_code()) 
          {
              case char_view("clear").hash_code():
              case char_view("print").hash_code():
              case char_view("list").hash_code():
                  break;
              default:
                  return false;
          }
          return true;
      }
    
  • implements special string literal for easy construction:

      constexpr auto s1 = "Test string"_cv;       
    

Other features

  • size of any char_view object is 8 for GCC x32 (size_t + one pointer)
  • does not use dynamic memory allocation in all cases when std::string is not involved
  • range checking & throwing on error is optional - implemented as policies (template parameters)
  • works with various char types: char, wchar_t, char16_t, char32_t
  • can be used with literals, char arrays, zstrings & std::string

Supported environments

  • compiler: any compiler with C++11 support (constexpr required)
  • tested with GCC 4.8.1 with C++11 support active (-std=c++11)
  • test build project prepared in Code::Blocks 13

External dependencies

  • just C++ standard library, including std::basic_string

Performance

In cases where code can be calculated in compile time (literals processing) functions using char_view will be much much faster than any other functions. In best cases functions return just single value calculated during compilation.

Known issues

  • not all overloads of std::string are implemented for functions like find_zzz (substr should be enough)
  • class does not check if provided "char *" pointer is literal or something else, so any char pointer can be provided and memory control must be done elsewhere

Other similar efforts

Contact information

Documentation

To build documentation, use Code::Blocks 13 and command:

DoxyBlocks \ Extract documentation

Output will be generated into: "./help/html/" directory in html format.

Release History

See the CHANGELOG.

About

literal & character buffer read-only view class for C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages