cstring_item.cpp (1193B)
1 #include "cstring_item.hpp" 2 #include "raw_item.hpp" 3 #include "../sink.hpp" 4 5 #include <libshit/string_utils.hpp> 6 7 namespace Neptools 8 { 9 10 CStringItem::CStringItem(Key k, Context& ctx, const Source& src) 11 : Item{k, ctx}, string{src.PreadCString(0)} 12 {} 13 14 CStringItem& CStringItem::CreateAndInsert(ItemPointer ptr) 15 { 16 auto x = RawItem::GetSource(ptr, -1); 17 return x.ritem.SplitCreate<CStringItem>(ptr.offset, x.src); 18 } 19 20 void CStringItem::Dump_(Sink& sink) const 21 { 22 sink.WriteCString(string); 23 } 24 25 void CStringItem::Inspect_(std::ostream& os, unsigned indent) const 26 { 27 Item::Inspect_(os, indent); 28 os << "c_string(" << Libshit::Quoted(string) << ')'; 29 } 30 31 std::string CStringItem::GetLabelName(std::string string) 32 { 33 size_t iptr = 0, optr = 0; 34 bool last_valid = false; 35 for (size_t len = string.length(); iptr < len && optr < 16; ++iptr) 36 if (isalnum(string[iptr])) 37 { 38 string[optr++] = string[iptr]; 39 last_valid = true; 40 } 41 else if (last_valid) 42 { 43 string[optr++] = '_'; 44 last_valid = false; 45 } 46 string.resize(optr); 47 return "str_" + string; 48 } 49 50 } 51 52 #include "cstring_item.binding.hpp"