char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
9e40df63b
char/aliceai-t5-moe
1#include "jinja/string.h" 2#include "jinja/value.h" 3 4#include <algorithm> 5#include <functional> 6#include <optional> 7#include <sstream> 8#include <string> 9#include <vector> 10 11namespace jinja { 12 13// 14// string_part 15// 16 17bool string_part::is_uppercase ()const { 18for (char c :val ) { 19if (std::islower (static_cast < unsigned char > (c ))) { 20return false; 21 } 22 } 23return true; 24} 25 26bool string_part::is_lowercase ()const { 27for (char c :val ) { 28if (std::isupper (static_cast < unsigned char > (c ))) { 29return false; 30 } 31 } 32return true; 33} 34 35// 36// string 37// 38 39void string::mark_input () { 40for (auto & part :parts ) { 41part .is_input = true; 42 } 43} 44 45std::string string::str ()const { 46if (parts .size ()== 1 ) { 47return parts [0 ].val ; 48 } 49 std::ostringstream oss ; 50for (const auto & part :parts ) { 51oss <<part .val ; 52 } 53return oss .str (); 54} 55 56size_t string::length ()const { 57size_t len = 0 ; 58for (const auto & part :parts ) { 59len += part .val .length (); 60 } 61return len ; 62} 63 64void string::hash_update (hasher & hash )const noexcept { 65for (const auto & part :parts ) { 66hash .update (part .val .data (),part .val .length ()); 67 } 68} 69 70bool string::all_parts_are_input ()const { 71for (const auto & part :parts ) { 72if (!part .is_input ) { 73return false; 74 } 75 } 76return true; 77} 78 79bool string::is_uppercase ()const { 80for (const auto & part :parts ) { 81if (!part .is_uppercase ()) { 82return false; 83 } 84 } 85return true; 86} 87 88bool string::is_lowercase ()const { 89for (const auto & part :parts ) { 90if (!part .is_lowercase ()) { 91return false; 92 } 93 } 94return true; 95} 96 97// mark this string as input if other has ALL parts as input 98void string::mark_input_based_on (const string & other ) { 99if (other .all_parts_are_input ()) { 100for (auto & part :parts ) { 101part .is_input = true; 102 } 103 } 104} 105 106string & string::append (const string & other ) { 107for (const auto & part :other .parts ) { 108parts .push_back (part ); 109 } 110return * this ; 111} 112 113// in-place transformation 114 115using transform_fn = std::function < std::string (const std::string & )> ; 116static string apply_transform (string & self ,const transform_fn & fn ) { 117for (auto & part :self .parts ) { 118part .val = fn (part .val ); 119 } 120return self ; 121} 122 123string string::uppercase () { 124return apply_transform (* this , [](const std::string & s ) { 125 std::string res = s ; 126 std::transform (res .begin (),res .end (),res .begin (), ::toupper ); 127return res ; 128 }); 129} 130string string::lowercase () { 131return apply_transform (* this , [](const std::string & s ) { 132 std::string res = s ; 133 std::transform (res .begin (),res .end (),res .begin (), ::tolower ); 134return res ; 135 }); 136} 137string string::capitalize () { 138return apply_transform (* this , [](const std::string & s ) { 139if (s .empty ())return s ; 140 std::string res = s ; 141res [0 ]= ::toupper (static_cast < unsigned char > (res [0 ])); 142 std::transform (res .begin ()+ 1 ,res .end (),res .begin ()+ 1 , ::tolower ); 143return res ; 144 }); 145} 146string string::titlecase () { 147return apply_transform (* this , [](const std::string & s ) { 148 std::string res = s ; 149bool capitalize_next = true; 150for (char & c :res ) { 151if (isspace (static_cast < unsigned char > (c ))) { 152capitalize_next = true; 153 }else if (capitalize_next ) { 154c = ::toupper (static_cast < unsigned char > (c )); 155capitalize_next = false; 156 }else { 157c = ::tolower (static_cast < unsigned char > (c )); 158 } 159 } 160return res ; 161 }); 162} 163string string::strip (bool left ,bool right , std::optional < const std::string_view > chars ) { 164static auto strip_part = [](const std::string & s ,bool left ,bool right , std::optional < const std::string_view > chars )-> std::string { 165size_t start = 0 ; 166size_t end = s .length (); 167auto match_char = [& chars ](unsigned char c )-> bool { 168return chars ? (* chars ).find (c )!= std::string::npos :isspace (c ); 169 }; 170if (left ) { 171while (start < end && match_char (static_cast < unsigned char > (s [start ]))) { 172++ start ; 173 } 174 } 175if (right ) { 176while (end > start && match_char (static_cast < unsigned char > (s [end - 1 ]))) { 177-- end ; 178 } 179 } 180return s .substr (start ,end - start ); 181 }; 182if (parts .empty ()) { 183return * this ; 184 } 185if (left ) { 186for (size_t i = 0 ;i < parts .size ();++ i ) { 187parts [i ].val = strip_part (parts [i ].val , true, false,chars ); 188if (parts [i ].val .empty ()) { 189// remove empty part 190parts .erase (parts .begin ()+ i ); 191-- i ; 192continue ; 193 }else { 194break ; 195 } 196 } 197 } 198if (right ) { 199for (size_t i = parts .size ();i -- > 0 ;) { 200parts [i ].val = strip_part (parts [i ].val , false, true,chars ); 201if (parts [i ].val .empty ()) { 202// remove empty part 203parts .erase (parts .begin ()+ i ); 204continue ; 205 }else { 206break ; 207 } 208 } 209 } 210return * this ; 211} 212 213}// namespace jinja