Skip to content

A C++ library, Py-Func, offering a collection of functions for various tasks. (split, range, count, replace, join)

License

Notifications You must be signed in to change notification settings

Thisal-D/py-func

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

py-func

Introduction

The py-func library provides a comprehensive collection of C++ implementations for common string, vector, and array manipulation tasks. This library aims to mimic some of the functionalities found in Python, making it easier to perform these operations in C++.

Features

  • String manipulation
  • Vector manipulation
  • Array manipulation

Usage

Including the Entire Library

To use the entire py-func library, include the master header file py-func.h in your C++ source files:

#include "py-func.h"

Including Individual Functions

If you only need specific functionalities, include the required headers separately:

#include "count.h"
#include "join.h"
#include "range.h"
#include "replace.h"
#include "slice.h"
#include "split.h"

Overview


  • 1. range

    The "range" functionality allows you to create a sequence of numbers or extract a sequence of values from iterable data types such as arrays, template arrays, vectors, and strings.

    1.1. Generate sequence of numbers

    • Allows you to generate sequences of numbers(vector) in C++, similar to Python's range() function. You can create sequences with specified start, end, and step values.

    pyfunc::range<type>(type start, type end, type step)

    • type

      int, long int, long long int, unsigned int, unsigned long int, float, double, long double
    • return type

      std::vector<type>

    • usage

      // Generate sequence of integers from 0 to 9 with a step of 1
      pyfunc::range<int>(0, 10, 1) -> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      
      // Generate sequence of unsigned integers from 0 to 8 with a step of 2
      pyfunc::range<unsigned int>(0, 10, 2) -> {0, 2, 4, 6, 8}
      
      // Generate sequence of long integers from -1 to -5 with a step of -1
      pyfunc::range<long int>(-1, -5, -1) -> {1, -2, -3, -4, -5}
      
      // Generate sequence of floats from 0.1 to 0.9 with a step of 0.1
      pyfunc::range<float>(0, 1, 0.1) -> {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
    • example

      for (int i: pyfunc::range(1, 10, 2)){
          std::cout << i << std::endl;
          /*
          1
          3
          5
          7
          9
          */
      }

    1.2. Slice std::string

    • It allows you to extract a subsequence from a given string based on specified start, end, and step values.

    pyfunc::slice(std::string source, long long start, long long end, long long step)

    • return type
      std::string

    • usage

      std::string string = "0123456789"
      
          pyfunc::slice(string, 0, 5, 1) -> "01234"
      
          pyfunc::slice(string, 0, 10, 2) -> "02468"
      
          pyfunc::slice(string, -1, -5, -1) -> "9876"
      
          pyfunc::slice(string, -1, -10, -2) -> "97531"
      
    • example

      std::string str = "1,2,3,4,5,6";
      
      std::cout << pyfunc::slice(str, 0, str.length(), 2); // 123456

    1.3. Slice std::vector

    • It allows you to extract a subsequence from a given vector based on specified start, end, and step values.

    pyfunc::slice(std::vector<type> source,long long start, long long end, long long step)

    • type

      any type
    • return type

      std::vector<type>

    • usage
      std::vector<int> numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      
          pyfunc::slice(numbers, 0, 5, 1) -> {0, 1, 2, 3, 4}
      
          pyfunc::slice(numbers, 0, 10, 2) -> {0, 2, 4, 6, 8}    
      
          pyfunc::slice(numbers, -1, -5, -1) -> {9, 8, 7, 6}
      
          pyfunc::slice(numbers, -1, -10, -2) -> {9, 7, 5, 3, 1}

    1.4. Slice array

    • It allows you to extract a subsequence from a given array based on specified start, end, and step values.

    pyfunc::slice(type *array, size_t size, long long start, long long end, long long step)

    • type

      any type
    • return type

      *any type

    • usage

      int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      
          pyfunc::slice(numbers, 10, 0, 5, 1) -> {0, 1, 2, 3, 4}
      
          pyfunc::slice(numbers, 10, 0, 10, 2) -> {0, 2, 4, 6, 8}
      
          pyfunc::slice(numbers, 10, -1, -5, -1) -> {9, 8, 7, 6}
      
          pyfunc::slice(numbers, 10, -1, -10, -2) -> {9, 7, 5, 3, 1}

      char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'}
      
          pyfunc::slice(characters, 5, 0, 3, 1) -> {'h', 'e', 'l'}
      
          pyfunc::slice(characters, 5, -1, -5, -1) -> {'o', 'l', 'l', 'e'}

      const char * characters = "hello"
      
          pyfunc::slice(characters, 5, 0, 3, 1) -> {'h', 'e', 'l'}
      
          pyfunc::slice(characters, 5, -1, -5, -1) -> {'o', 'l', 'l', 'e'}

  • 2. replace

    • replaces a specified value with another value in Iterable data types eg:- arrays, template arrays, vectors and strings.

    2.1. replace strings

    pyfunc::replace(std::string source, type1 search, type2 replace)

    • type1

      std::string, char
    • type2

      char, std::string
    • return type

      std::string

    • usage
      std::string str = "my name is Name, Name is 10 years old."
      
          pyfunc::replace(str, "Name", "john Watson") -> "john Watson is 10 years old.john Watson is 50 years old."
      
          pyfunc::replace(str, '1', "5") -> "my name is Name, Name is 50 years old."

    2.2. replace vectors

    pyfunc::replace<type>(std::vector<type>, type search, type replace)

    • type

      any type
    • return type

      std::vector<any type>

    • usage

      std::vector<int> numbers = {1, 1, 2, 2, 3, 4, 5, 5, 6}
      
          pyfunc::replace(numbers, 1, 100) -> {100, 100, 2, 2, 3, 4, 5, 5, 6}
      
          pyfunc::replace(numbers, 5, 1) -> {1, 1, 2, 2, 3, 4, 1, 1, 6}

      std::vector<std::string> strs = {"abc", "def", "ghi"}
      
          pyfunc::replace(strs, "abc", "replaced") -> {"replaced", "def", "ghi"}
      
          pyfunc::replace<std::string>(strs, "def", "replaced") ->  {"abc", "def", "replaced"}

    2.3. replace arrays

    pyfunc::replace(type *array, size_t size, type search, type replace)

    • type

      any type
    • return type

      *any type

    • usage

      int numbers[10] = {0, 0, 0, 1, 2, 3, 3, 4, 4, 5}
      
          pyfunc::replace(numbers,10, 0, 100) -> {100, 100, 100, 1, 2, 3, 3, 4, 4, 5}

      char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'}
      
          pyfunc::replace(characters, 5, 'l', 'L') -> {'h', 'e', 'L', 'L', 'o', '\0'}

    2.4. replace templatize arrays

    pyfunc::replace(std::array<type, size_t size>, type search, type replace)

    • type

      any type
    • return type

      std::array<any type, size>

    • usage

      std::array<int, 5> numbers= {0, 0, 0, 1, 2}
      
          pyfunc::replace(numbers, 0, 100) -> {100, 100, 100, 1, 2}

      std::array<char, 5> characters= {'h', 'e', 'l', 'l', 'o'}
      
          pyfunc::replace(characters, 'l', 'L') -> {'h', 'e', 'L', 'L', 'o', '\0'}

  • 3. split

    • Split a std::string into a std::vector< std::string> where each word is a list item

    pyfunc::split(std::string source, type splitter)

    • type

      char, std::string
    • return type

      std::vector<std::string>

    • usage

      std::string str = "1,2,3,4,5";`
      
          pyfunc::split(str, ",") -> {"1", "2", "3", "4", "5"}

      std::string str = "1>>432>>3>>4213>>5"
      
          pyfunc::split(str, ">>") -> {"1", "432", "3", "4213", "5"}
      
          pyfunc::split(str, '>') -> {"1", "", "432", "", "3", "", "4213", "", "5"}

      std::string str = "c:/users/user/appdata";`
      
          pyfunc::split(str, "/") -> {"c:", "users", "user", "appdata"}

  • 4. count

    • The count method returns the number of elements with the specified value in Iterable. eg:- arrays, template arrays, vectors and strings

    4.1. count in strings

    pyfunc::count<type2>(std::string source, type search)

    • type

      std::string, char
    • type2

      int, long int, long long int, unsigned long, unsigned long long, double, float 

    • usage
        std::string str = "11234123"
    
            pyfunc::count<int>(str, "1") -> 3
    
            pyfunc::count<long long int>(str, "2") -> 2
    
            pyfunc::count<int>(str, "123") -> 2

    4.2. count in vectors

    pyfunc::count<type, type2>(std::vector<type>, type search)

    • type

      any type
    • type2

      int, long int, long long int, unsigned long, unsigned long long, double, float 

    • usage

      std::vector<int> numbers = {1, 1, 2, 2, 3, 4, 5, 5, 6}
      
          pyfunc::count<int, int>(numbers, 1) -> 2
      
          pyfunc::count<int, int>(numbers, 6) -> 1

      std::vector<std::string> strs = {"abc", "def", "ghi", "abc", "abc"}
      
          pyfunc::count<std::string, long long int>(strs, "abc") -> 3

    4.3. count in arrays

    pyfunc::count<type, type2>(type *array, size_t size, type search)

    • type

      any type
    • type2

      int, long int, long long int, unsigned long, unsigned long long, double, float 
    • return type

      unsigned long long

    • usage

      int numbers[10] = {0, 0, 0, 1, 2, 3, 3, 4, 4, 1}
      
          pyfunc::count<int, int>(numbers, 10, 0) -> 3

      char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'}
      
          pyfunc::count<char, int>(characters, 5, 'l') -> 2

    4.4 . count in templatize arrays

    pyfunc::count<type, size, type2>(std::array<type, size_t size>, type search)

    • type

      any type
    • type2

      int, long int, long long int, unsigned long, unsigned long long, double, float 

    • usage

      std::array<int, 5> numbers= {0, 0, 0, 1, 2}
      
          pyfunc::count<int, 5, int>(numbers, 0) -> 3
      
          pyfunc::count<int, 5, long int>(numbers, 0) -> 3  

      std::array<char, 5> characters= {'h', 'e', 'l', 'l', 'o'}
      
          pyfunc::count<char, 5, int>(characters, 'l') -> 3

  • 5 . join

    • The count method returns the number of elements with the specified value in Iterable. eg:- arrays, template arrays, vectors and strings

    5.1 . join strings

    pyfunc::join(std::string source, type join_by)

    • type
      std::string, char

    • usage

      std::string str = "2468";
      
          pyfunc::join(str, ",") -> "2,4,6,8"
      
          pyfunc::join(str, "--") -> "2--4--6--8"
      
          pyfunc::join(str, '|') -> "2|4|6|8"

    5.2 . join

    pyfunc::join(std::vector<type> source, type join_by)

    • type
      std::string, char

    • usage

      std::vector<std::string> strs = {"abc","def","ghi","jkl"}
      
          pyfunc::join(strs, "+") -> "abc+def+ghi+jkl"
      
          pyfunc::join(strs, "_____") -> "abc_____def_____ghi_____jkl"
      
          pyfunc::join(strs, '|') -> "abc|def|ghi|jkl"

    5.3 . join arrays

    pyfunc::join(type *array, size_t size, type join_by);

    • type

      std::string, char
    • return type

      std::string

    • usage

      std::string strs[] = {"abc","def","ghi","jkl"}
      
          pyfunc::join(strs, 4, "||||") -> "abc||||def||||ghi||||jkl"
      
          pyfunc::join(strs, 4, "-") -> "abc-def-ghi-jkl" 

      char characters[6] = {'h', 'e', 'l', 'l', 'o', '\0'}
      
          pyfunc::join(characters, 5, ',') -> "h,e,l,l,o"

    5.4 . join templatize arrays

    pyfunc::join(std::array<type, size_t size>, type join_by)

    • type

      std::string, char
    • return type

      std:string

    • usage

      std::array<char, 5> characters = {'0', '0', '0', '1', '2'}
      
          pyfunc::join(characters, '=') -> "0=0=0=1=2"

      std::array<std::string, 4> strs = {"a","b","c","d"}
      
          pyfunc::join(strs, ">") -> "a>b>c>d"

About

A C++ library, Py-Func, offering a collection of functions for various tasks. (split, range, count, replace, join)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published