Various C++ functions that ol' Bjarne forgot to include
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
barney.h
barney.jpeg

README.md

Barney Sastrup

Barney

Barney is a library of various C++ functions that make C++ programming a little bit less painful. These functions are designed to be fairly idiot-proof and many will try to auto correct if given unreasonable parameters.

To use Barney in your project, just add the barney.h file to your project directory and

#include "barney.h"

If you do not wish to include the entire header file, you can also copy the individual functions that you need, but note that some functions are dependent on others.

The functions in the source code are sorted in the same order as they appear here.

Contents

Ranges

range

Inspired by Python, the range function produces a vector of integers given upper and lower limits and a step amount.

range(5); //{0, 1, 2, 3, 4}
range(3, 6); //{3, 4, 5}
range(4, 10, 2); //{4, 6, 8}
range(0, -10, -2); //{0, 2, -4, -6, -8}

The vector that is produced can be used in C++11 ranged for loops and can be used to replace conventional for loops.

//Instead of
for(int i = 0; i < size; i++);

//You can use
for(int i : range(size));

The range function can also find the range between two characters, like in Ruby.

range('a', 'e'); //{'a', 'b', 'c', 'd'}

rangei

The rangei function does the same as range, but includes the last item.

rangei(5); //{0, 1, 2, 3, 4, 5}
rangei('a', 'e'); //{'a', 'b', 'c', 'd', 'e'}

Strings

These string manipulation functions are inspired by the string manipulation functions in JavaScript and Python.

tostring

The tostring function converts any type that can be printed via cout to a string.

tostring(1); //"1"
tostring('a'); //"a"
tostring("Already a string"); //"Already a string"

substring

The substring function gets a string between two indices. If only one index is given, it gets the entire string from the starting index. If given a negative index, it will select from the end.

substring("Hello world!", 1, 4); //"ell"
substring("Hello world!", 1); //"ello world!"

split

The split function splits a string into a vector of strings given a string or character as a delimiter. Delimiters can be multiple characters in length.

split("How are you doing today?", " "); //{"How", "are", "you", "doing", "today?"}

join

The join function joins a vector of strings into a string given a string or character as a delimiter. Delimiters can be multiple characters in length.

join({"How", "are", "you", "doing", "today?"}, " "); //"How are you doing today?";

replace

The replace function replaces all instances of one string or character with another string or character. Leaving the third parameter blank will delete all instances of the first string.

replace("I have a blue house and a blue car", "blue", "red"); //"I have a red house and a red car"
replace("I have a blue house and a blue car", "blue"); //"I have a house and a car"

strip

The strip function removes all leading and trailing whitespace.

strip("\n Hello World! \n"); //"Hello World!"

index

The index function finds the index of the first occurence of a character or string. It returns -1 if no matches are found.

index("Hello world, welcome to the universe.", "e"); //1
index("Hello world, welcome to the universe.", "z"); //-1

lastindex

The lastindex function finds the index of the last occurence of a character or string. It returns -1 if no matches are found.

lastindex("Hello world, welcome to the universe.", "e"); //35
lastindex("Hello world, welcome to the universe.", "z"); //-1

indices

The indices function finds the indices of all occurences of a character or string. It returns a vector.

indices("Hello world, welcome to the universe.", "e"); //{1, 14, 19, 26, 32, 35}
indices("Hello world, welcome to the universe.", "z"); //{}

includes

The includes function returns whether or not a string contains a given string or character.

includes("Hello world", "world"); //true
includes("Hello world", "test"); //false

startswith

The startswith function returns whether a string starts with a given string or character.

startswith("Hello world", "Hello"); //true
startswith("Hello world", "world"); //false

endswith

The endswith function returns whether a string ends with a given string or character.

endswith("Hello world", "world"); //true
endswith("Hello world", "Hello"); //false

repeat

The repeat function produces a string containing a specified number of repetitions of a string or character.

repeat("test", 3); //"testtesttest"

Vectors

These vector manipulation functions are inspired by the array manipulation functions in JavaScript and offer similar functionality to the aforementioned string functions.

slice

The slice function returns the part of a vector between two indices. If only one index is given, it gets the entire vector from the starting index. If given a negative index, it will select from the end.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
slice(example, 1, 3); //{"Orange", "Lemon"}

splice

The splice function adds specified items and/or removes a specified number of items from a vector at a given index. If given a negative index, it will select from the end.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
splice(example, 2, 1); //{"Banana", "Orange", "Apple", "Mango"}
vector<string> additions {"Pineapple", "Kiwi"};
splice(example, 2, 1, additions); //{"Banana", "Orange", "Pineapple", "Kiwi", "Apple", "Mango"}
splice(example, 2, additions); //{"Banana", "Orange", "Pineapple", "Kiwi", "Lemon", "Apple", "Mango"}

index

The index function finds the index of the first occurence of an item. It returns -1 if no matches are found.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
index(example, string("Orange")); //1
index(example, string("Carrot")); //-1

lastindex

The lastindex function finds the index of the last occurence of an item. It returns -1 if no matches are found.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
lastindex(example, string("Apple")); //3
lastindex(example, string("Carrot")); //-1

indices

The indices function finds the indices of all occurences of an item. It returns a vector.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
indices(example, string("Apple")); //{4}
indices(example, string("Carrot")); //{}

includes

The includes function returns whether or not a vector contains a given item.

vector<string> example {"Banana", "Orange", "Lemon", "Apple", "Mango"};
includes(example, "Orange"); //true
includes(example, "Tomato"); //false

More coming soon 😎