str#

class numpy::str_#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use str_
str_ obj;
// ... operations ...

Constructors#

Signature

Location

Example

str\_(const std::string &s)

NP_STRING_TYPES.H:22

str\_(const char \*s)

NP_STRING_TYPES.H:27

str\_(const str\_ &other)

NP_STRING_TYPES.H:36

Operators#

Signature

Return Type

Location

Example

str\_ & operator=(const str\_ &other)

str_ &

NP_STRING_TYPES.H:41

str\_ & operator=(const std::string &s)

str_ &

NP_STRING_TYPES.H:49

operator std::string()

NP_STRING_TYPES.H:56

bool operator==(const str\_ &other)

bool

NP_STRING_TYPES.H:61

bool operator!=(const str\_ &other)

bool

NP_STRING_TYPES.H:65

bool operator<(const str\_ &other)

bool

NP_STRING_TYPES.H:69

bool operator<=(const str\_ &other)

bool

NP_STRING_TYPES.H:73

bool operator>(const str\_ &other)

bool

NP_STRING_TYPES.H:77

bool operator>=(const str\_ &other)

bool

NP_STRING_TYPES.H:81

str\_ operator+(const str\_ &other)

str_

NP_STRING_TYPES.H:86

str\_ & operator+=(const str\_ &other)

str_ &

NP_STRING_TYPES.H:91

str\_ operator-(const str\_ &other)

str_

NP_STRING_TYPES.H:99

str\_ operator\*(const str\_ &other)

str_

NP_STRING_TYPES.H:104

str\_ operator/(const str\_ &other)

str_

NP_STRING_TYPES.H:109

char & operator[](size_tpos)

char &

NP_STRING_TYPES.H:121

const char & operator[](size_tpos)

const char &

NP_STRING_TYPES.H:125

Array Creation#

Signature

Return Type

Location

Example

bool empty()

bool

NP_STRING_TYPES.H:119

View

Indexing / Selection#

Signature

Return Type

Location

Example

char at_safe(size_tpos)

char

NP_STRING_TYPES.H:140

View

Other Methods#

Signature

Return Type

Location

Example

const char \* c_str()

const char *

NP_STRING_TYPES.H:115

View

void clear()

void

NP_STRING_TYPES.H:171

View

size_t find(const str\_ &s)

size_t

NP_STRING_TYPES.H:166

View

size_t length()

size_t

NP_STRING_TYPES.H:116

View

size_t size()

size_t

NP_STRING_TYPES.H:117

View

str\_ slice(intstart, intstop = -1)

str_

NP_STRING_TYPES.H:148

View

str\_ substr(size_tpos, size_tlen = N)

str_

NP_STRING_TYPES.H:130

View

Code Examples#

The following examples are extracted from the test suite.

empty (np_test_1_all.cpp:6316)
6306}
6307
6308void test_data_generator_emptyBenchmarkSorting() {
6309    std::cout << "========= test_data_generator_empty =======================";
6310
6311    DataGenerator<int> gen(42);
6312
6313    // Test empty data generation
6314    std::vector<int> data = gen.generate(0, DataPattern::RANDOM);
6315
6316    if (!(data.empty())) {
6317        std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())";
6318        std::cout << std::string("[FAIL] ") + description << std::endl;
6319        throw std::runtime_error(description);
6320    }
6321
6322    // std::cout << "Empty data generation test passed" << std::endl;
6323
6324    std::cout << " -> tests passed" << std::endl;
6325}
at_safe (np_test_1_all.cpp:13109)
13099    std::cout << " -> tests passed" << std::endl;
13100  }
13101
13102  void test_character_access_bounds() {
13103    std::cout << "========= test_character_access_bounds =======================";
13104
13105    vstring_ str1("abc");
13106
13107    // Test safe character access within bounds
13108    try {
13109      char c1 = str1.at_safe(0);  // Should work
13110      if (!(c1 == 'a')) {
13111          std::string description = std::string("test_character_access_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(c1 == 'a')";
13112          std::cout << std::string("[FAIL] ") + description << std::endl;
13113          throw std::runtime_error(description);
13114      }
13115      // std::cout << "[OK] at_safe(0) on 'abc' returns 'a'\n";
13116
13117      char c2 = str1.at_safe(2);  // Should work
13118      if (!(c2 == 'c')) {
13119          std::string description = std::string("test_character_access_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(c2 == 'c')";
c_str (np_test_1_all.cpp:13064)
13054  }
13055
13056  void test_fixed_size_string_out_of_bounds() {
13057    std::cout << "========= test_fixed_size_string_out_of_bounds =======================";
13058
13059    // Test str_<N>
13060    str_<10> str1("hello");  // length = 5, capacity = 10
13061
13062    // Test substr out-of-bounds
13063    auto result1 = str1.substr(20, 5);
13064    if (!(std::string(result1.c_str()) == "")) {
13065        std::string description = std::string("test_fixed_size_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::string(result1.c_str()) == \"\")";
13066        std::cout << std::string("[FAIL] ") + description << std::endl;
13067        throw std::runtime_error(description);
13068    }
13069    // std::cout << "[OK] str_<10> substr(20, 5) on 'hello' returns empty string\n";
13070
13071    // Test slice out-of-bounds
13072    auto result2 = str1.slice(10, 15);
13073    if (!(std::string(result2.c_str()) == "")) {
13074        std::string description = std::string("test_fixed_size_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::string(result2.c_str()) == \"\")";
clear (np_test_2_all.cpp:4161)
4151        auto array = createFloat32Array({ 10, 10 }, static_cast<float>(i));
4152        temp_arrays.push_back(array);
4153
4154        auto view = array.view();
4155        temp_views.push_back(view);
4156      }
4157
4158      // std::cout << "Created 100 arrays and 100 views" << std::endl;
4159
4160      // Clear all arrays (views will keep memory alive)
4161      temp_arrays.clear();
4162      // std::cout << "Cleared original arrays" << std::endl;
4163
4164      // Verify views still work
4165      if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) {
4166          std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)";
4167          std::cout << std::string("[FAIL] ") + description << std::endl;
4168          throw std::runtime_error(description);
4169      }
4170      // std::cout << "[OK] Views still valid after originals cleared";
find (np_test_1_all.cpp:1324)
1314    // Test incompatible shapes
1315    auto a = createFloat64Array({2, 3});
1316    auto b = createFloat64Array({4});  // Incompatible for broadcasting
1317
1318    bool caught_exception = false;
1319    try {
1320        auto result = greater(a, b);
1321    } catch (const std::invalid_argument& e) {
1322        caught_exception = true;
1323        std::string msg(e.what());
1324        bool has_msg = msg.find("incompatible shapes") != std::string::npos;
1325        if (has_msg != true) {
1326            std::cout << "[FAIL] in testComparisonErrorHandling(): exception message missing 'incompatible shapes', got: " << msg << std::endl;
1327            throw std::runtime_error("testComparisonErrorHandling(): exception message incorrect");
1328        }
1329    }
1330    if (caught_exception != true) {
1331        std::cout << "[FAIL] in testComparisonErrorHandling(): expected exception not thrown" << std::endl;
1332        throw std::runtime_error("testComparisonErrorHandling(): shape incompatibility exception not thrown");
1333    }
1334    // std::cout << "[OK] Shape incompatibility exception handling" << std::endl;
length (np_test_1_all.cpp:7335)
7325    std::cout << " -> tests passed" << std::endl;
7326}
7327
7328void testStringTruncationStringTypes() {
7329    std::cout << "========= testStringTruncationStringTypes =======================";
7330
7331    // Test truncation behavior
7332    str8 small("This string is way too long for 8 characters");
7333    // std::cout << "Long string in str8: '" << small << "'" << std::endl;
7334    // std::cout << "Length: " << small.length() << " (max: " << str8::max_size << ")" << std::endl;
7335
7336    str16 medium("This is a medium length string that should be truncated");
7337    // std::cout << "Medium string in str16: '" << medium << "'" << std::endl;
7338    // std::cout << "Length: " << medium.length() << " (max: " << str16::max_size << ")" << std::endl;
7339
7340    std::cout << " -> tests passed" << std::endl;
7341}
7342
7343void testStringComparisonsStringTypes() {
7344    std::cout << "========= testStringComparisonsStringTypes =======================";
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43    const double TOLERANCE = 1e-10;
44
45    bool allTrue(const NDArray<bool>& arr) {
46        std::vector<size_t> indices(arr.getShape().size(), 0);
47        do {
48            if (!arr.getElementAt(indices)) {
49                return false;
50            }
51        } while (incrementIndices(indices, arr.getShape()));
52        return true;
53    }
54
55    bool allFalse(const NDArray<bool>& arr) {
56        std::vector<size_t> indices(arr.getShape().size(), 0);
slice (np_test_1_all.cpp:13009)
12999    // Test substr valid start but excessive length
13000    auto result3 = str1.substr(2, 10);  // start=2, len=10 on string of length 5
13001    if (!(result3.to_string() == "llo")) {
13002        std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")";
13003        std::cout << std::string("[FAIL] ") + description << std::endl;
13004        throw std::runtime_error(description);
13005    }
13006    // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n";
13007
13008    // Test slice method with out-of-bounds
13009    auto result4 = str1.slice(10, 15);  // slice[10:15] on string of length 5
13010    if (!(result4.to_string() == "")) {
13011        std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")";
13012        std::cout << std::string("[FAIL] ") + description << std::endl;
13013        throw std::runtime_error(description);
13014    }
13015    // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n";
13016
13017    // Test slice with negative indices
13018    auto result5 = str1.slice(-2, -1);  // slice[-2:-1] should be "l"
13019    if (!(result5.to_string() == "l")) {
substr (np_test_1_all.cpp:7373)
7363    str32 s2(" World");
7364
7365    str32 s3 = s1 + s2;
7366    // std::cout << "'" << s1 << "' + '" << s2 << "' = '" << s3 << "'" << std::endl;
7367
7368    s1 += s2;
7369    // std::cout << "After +=: '" << s1 << "'" << std::endl;
7370
7371    // Test substring
7372    str32 full("Programming");
7373    str32 sub = full.substr(0, 7);
7374    // std::cout << "Substring of '" << full << "' (0,7): '" << sub << "'" << std::endl;
7375
7376    // Test find
7377    str32 search("Hello World Programming");
7378    size_t pos = search.find(str32("World"));
7379    // std::cout << "Position of 'World' in '" << search << "': " << pos << std::endl;
7380
7381    // Test utility functions
7382    str32 lower = to_lower(str32("UPPERCASE"));
7383    str32 upper = to_upper(str32("lowercase"));