//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- String ShaderSourceRepository::shaderSource(ShaderIdent shaderIdent) { String shaderProg; CharArray rawSource; if (rawShaderSource(shaderIdent, &rawSource)) { if (rawSource.size() > 0) { #ifdef CVF_OPENGL_ES // Always version 100 on OpenGL ES shaderProg = "#version 100\nprecision highp float;\n"; #else // Default on desktop is GLSL 1.2 (OpenGL 2.1) unless the shader explicitly specifies a version if (rawSource[0] != '#') { shaderProg = "#version 120\n"; } #endif shaderProg += rawSource.ptr(); } } return shaderProg; }
/// Return a random unicode term, like StressIndexingTest. String randomString() { int32_t end = random->nextInt(20); if (buffer.size() < 1 + end) { buffer.resize((int32_t)((double)(1 + end) * 1.25)); } for (int32_t i = 0; i < end; ++i) { int32_t t = random->nextInt(5); if (t == 0 && i < end - 1) { #ifdef LPP_UNICODE_CHAR_SIZE_2 // Make a surrogate pair // High surrogate buffer[i++] = (wchar_t)nextInt(0xd800, 0xdc00); // Low surrogate buffer[i] = (wchar_t)nextInt(0xdc00, 0xe000); #else buffer[i] = (wchar_t)nextInt(0xdc00, 0xe000); #endif } else if (t <= 1) { buffer[i] = (wchar_t)nextInt(0x01, 0x80); } else if (t == 2) { buffer[i] = (wchar_t)nextInt(0x80, 0x800); } else if (t == 3) { buffer[i] = (wchar_t)nextInt(0x800, 0xd800); } else if (t == 4) { buffer[i] = (wchar_t)nextInt(0xe000, 0xfff0); } } return String(buffer.get(), end); }
void test_set_byte() { CharArray* c = String::create(state, "xyz")->data(); c->set_byte(state, Fixnum::from(0), Fixnum::from('1')); TS_ASSERT_EQUALS(c->get_byte(state, Fixnum::from(0)), Fixnum::from('1')); c->set_byte(state, Fixnum::from(2), Fixnum::from('2')); TS_ASSERT_EQUALS(c->get_byte(state, Fixnum::from(2)), Fixnum::from('2')); }
value_t Hash::to_s(Hash *self) { RecursionDetector<RecursionType::Hash_to_s, false> rd(self); if(rd.recursion()) return String::get("{...}"); CharArray result = "{"; OnStack<1> os1(self); OnStackString<1> os2(result); HashAccess::each_pair(self, [&](value_t key, value_t value) -> bool { OnStack<1> os(value); result += inspect(key); result += "=>"; result += inspect(value); result += ", "; return true; }); if(result.size() > 1) result.shrink(result.size() - 2); result += "}"; return result.to_string(); }
value_t IO::rb_readlines(String *path) { Stream *file; Platform::wrap([&] { file = Platform::open(path->string, Platform::Read, Platform::Open); }); Finally finally([&] { delete file; }); auto result = Array::allocate(); while(true) { CharArray line = file->gets(); if(line.size()) result->vector.push(line.to_string()); else break; } return result; }
String* String::resize_capacity(STATE, Fixnum* count) { native_int sz = count->to_native(); if(sz < 0) { Exception::argument_error(state, "negative byte array size"); } else if(sz >= INT32_MAX) { // >= is used deliberately because we use a size of + 1 // for the byte array Exception::argument_error(state, "too large byte array size"); } CharArray* ba = CharArray::create(state, sz + 1); native_int copy_size = sz; native_int data_size = as<CharArray>(data_)->size(); // Check that we don't copy any data outside the existing byte array if(unlikely(copy_size > data_size)) { copy_size = data_size; } memcpy(ba->raw_bytes(), byte_address(), copy_size); // We've unshared shared(state, Qfalse); data(state, ba); hash_value(state, nil<Fixnum>()); // If we shrunk it and num_bytes said there was more than there // is, clamp it. if(num_bytes()->to_native() > sz) { num_bytes(state, count); } return this; }
void test_to_chars() { String* s = String::create(state, "xy"); CharArray* c = s->data(); char* chars = c->to_chars(state, Fixnum::from(2)); TS_ASSERT_SAME_DATA("xy", chars, 2); }
void IL2Client::game_TargetByName( const wchar_t *name ) { //L2Client *cl = (L2Client *)m_pcl; UserInfo *usr = get_UserInfo(); CharArray *ca = get_WorldChars(); NpcArray *na = get_WorldNpcs(); unsigned int nPassed = 0, i = 0, count = ca->GetCount(); unsigned int objectID = 0; if( _wcsicmp( name, usr->charName ) == 0 ) { objectID = usr->objectID; } if( objectID == 0 ) { // try chars count = ca->GetCount(); nPassed = 0; if( count > 0 ) { ca->Lock(); for( i=0; i<CharArray::CHARARRAY_MAX_CHARS; i++ ) { if( ca->chars_array[i]->isUnused() ) continue; nPassed++; if( _wcsicmp( name, ca->chars_array[i]->charName ) == 0 ) { objectID = ca->chars_array[i]->objectID; break; } if( nPassed >= count ) break; } ca->Unlock(); } } if( objectID == 0 ) { // try NPCs count = na->getCount(); nPassed = 0; if( count > 0 ) { na->Lock(); for( i=0; i<NpcArray::NPCA_MAX_NPCS; i++ ) { if( na->npcs_array[i]->isUnused() ) continue; nPassed++; if( _wcsicmp( name, na->npcs_array[i]->charName ) == 0 ) { objectID = na->npcs_array[i]->objectID; break; } if( nPassed >= count ) break; } na->Unlock(); } } if( objectID == 0 ) return; game_Action( objectID ); }
int32_t StringUtils::toUnicode(const uint8_t* utf8, int32_t length, CharArray unicode) { if (length == 0) return 0; UTF8Decoder utf8Decoder(utf8, utf8 + length); int32_t decodeLength = utf8Decoder.decode(unicode.get(), unicode.size()); return decodeLength == Reader::READER_EOF ? 0 : decodeLength; }
void test_get_byte_index_out_of_bounds() { CharArray* c = String::create(state, "xyz")->data(); native_int sz = c->size(state)->to_native(); TS_ASSERT_THROWS_ASSERT(c->get_byte(state, Fixnum::from(sz)), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); TS_ASSERT_THROWS_ASSERT(c->get_byte(state, Fixnum::from(sz+1)), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); TS_ASSERT_THROWS_ASSERT(c->get_byte(state, Fixnum::from(-1)), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); }
void test_compare_bytes_out_of_bounds() { CharArray* a = String::create(state, "xyZzy")->data(); CharArray* b = String::create(state, "xyzzy")->data(); Fixnum* zero = Fixnum::from(0); Fixnum* neg = Fixnum::from(-1); TS_ASSERT_THROWS_ASSERT(a->compare_bytes(state, b, neg, zero), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); TS_ASSERT_THROWS_ASSERT(a->compare_bytes(state, b, zero, neg), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); }
CharArray Stream::gets() { static const size_t buffer_size = 0x200; CharArray result, buffer; while(true) { pos_t p = pos(); buffer = read(buffer_size); for(size_t i = 0; i < buffer.size(); ++i) { if(buffer[i] == '\r') { ++i; if(i < buffer.size()) { if(buffer[i] == '\n') ++i; seek(p + i, FromStart); return result + buffer.copy(0, i); } else { CharArray c = read(1); if(c == "\n") buffer += c; else seek(-(pos_t)c.size()); return result + buffer; } } else if(buffer[i] == '\n') { ++i; seek(p + i, FromStart); return result + buffer.copy(0, i); } } if(buffer.size() < buffer_size) return result + buffer; result += buffer; } }
CharArray NativeStream::read(size_t length) { CharArray buf; buf.buffer(length); ssize_t result = ::read(fd, buf.str_ref(), buf.size()); if(result < 0) raise("Unable to read from file descriptor"); buf.shrink((size_t)result); return buf; }
ByteArray Convert::FromBase64CharArray(CharArray& inArray, int offset, int length) { if(inArray.IsNull()) throw ArgumentNullException(L"inArray"); if(offset < 0) throw ArgumentOutOfRangeException(L"offset < 0"); if(length < 0) throw ArgumentOutOfRangeException(L"length < 0"); // avoid integer overflow if(offset > (int)inArray.Length() - length) throw ArgumentOutOfRangeException(L"offset + length > array.Length"); return InternalFromBase64CharArray(inArray, offset, length); }
int Convert::ToBase64CharArray(ByteArray& inArray, int offsetIn, int length, CharArray& outArray, int offsetOut) { using namespace Security; if(inArray.IsNull()) throw ArgumentNullException(L"inArray"); if (outArray.IsNull()) throw ArgumentNullException(L"outArray"); if(offsetIn < 0 || length < 0 || offsetOut < 0) throw ArgumentOutOfRangeException(L"offsetIn, length, offsetOut < 0"); // avoid integer overflow if(offsetIn > (int)inArray.Length() - length) throw ArgumentOutOfRangeException(L"offsetIn + length > array.Length"); // note: normally ToBase64Transform doesn't support multiple block processing ByteArray outArr = Cryptography::Base64Helper::TransformFinalBlock(inArray, offsetIn, length); Text::ASCIIEncoding enc; CharArray cOutArr = enc.GetChars(outArr); // avoid integer overflow if(offsetOut > (int)(outArray.Length() - cOutArr.Length()) ) throw ArgumentOutOfRangeException(L"offsetOut + cOutArr.Length > outArray.Length"); outArray.Base(offsetOut); for(int32 i = 0; i < (int32)cOutArr.Length(); ++i) outArray[i] = cOutArr[i]; outArray.Base(0); return (int)cOutArr.Length(); }
void test_collect_young_copies_chararray_bodies() { ObjectMemory& om = *state->vm()->om; CharArray* obj; obj = CharArray::create(state, 3); obj->raw_bytes()[0] = 48; Root r(roots, obj); om.collect_young(*gc_data); obj = (CharArray*)roots->front()->get(); TS_ASSERT_EQUALS(obj->raw_bytes()[0], static_cast<char>(48)); }
TEST_F(MappingCharFilterTest, testReaderReset) { CharStreamPtr cs = newLucene<MappingCharFilter>(normMap, newLucene<StringReader>(L"x")); CharArray buf = CharArray::newInstance(10); int32_t len = cs->read(buf.get(), 0, 10); EXPECT_EQ(1, len); EXPECT_EQ(L'x', buf[0]) ; len = cs->read(buf.get(), 0, 10); EXPECT_EQ(-1, len); // rewind cs->reset(); len = cs->read(buf.get(), 0, 10); EXPECT_EQ(1, len); EXPECT_EQ(L'x', buf[0]) ; }
template<typename F1, typename F2> void split(const CharArray &input, F1 data, F2 split) { compile_pattern(); int ovector[vector_size]; size_t prev = 0; while(true) { int groups = match(input, ovector, prev); if(groups <= 0) { if(prev < input.size()) data(input.copy(prev, input.size() - prev)); return; } else { int start = ovector[0]; int stop = ovector[1]; for (int i = 0; i < groups; i++) { start = std::min(start, ovector[2 * i]); stop = std::max(stop, ovector[2 * i + 1]); } if((size_t)stop == prev) { split(start, stop); data(input.copy(prev, 1)); prev += 1; } else { data(input.copy(prev, (size_t)start - prev)); split(start, stop); prev = stop; } } } }
String* String::create_reserved(STATE, native_int bytes) { String *so; so = state->new_object<String>(G(string)); so->num_bytes(state, Fixnum::from(0)); so->hash_value(state, nil<Fixnum>()); so->shared(state, Qfalse); CharArray* ba = CharArray::create(state, bytes+1); ba->raw_bytes()[bytes] = 0; so->data(state, ba); return so; }
void test_fetch_bytes_out_of_bounds() { CharArray* c = String::create(state, "xyzzy")->data(); Fixnum* neg = Fixnum::from(-1); Fixnum* zero = Fixnum::from(0); Fixnum* one = Fixnum::from(1); Fixnum* size = c->size(state); TS_ASSERT_THROWS_ASSERT(c->fetch_bytes(state, neg, zero), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); TS_ASSERT_THROWS_ASSERT(c->fetch_bytes(state, zero, neg), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); TS_ASSERT_THROWS_ASSERT(c->fetch_bytes(state, one, size), const RubyException &e, TS_ASSERT(Exception::object_bounds_exceeded_error_p(state, e.exception))); }
/* * Creates a String instance with +num_bytes+ bytes of storage. * It also pins the CharArray used for storage, so it can be passed * to an external function (like ::read) */ String* String::create_pinned(STATE, Fixnum* size) { String *so; so = state->new_object<String>(G(string)); so->num_bytes(state, size); so->hash_value(state, nil<Fixnum>()); so->shared(state, Qfalse); native_int bytes = size->to_native() + 1; CharArray* ba = CharArray::create_pinned(state, bytes); ba->raw_bytes()[bytes-1] = 0; so->data(state, ba); return so; }
value_t rb_each_line(IO *io, value_t block) { OnStack<2> os(io, block); while(true) { io->assert_stream(); CharArray line = io->stream->gets(); if(line.size()) yield(block, line.to_string()); else break; } return io; }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- String ShaderSourceProvider::getSourceFromFile(String shaderName) { const String fileName = shaderName + ".glsl"; size_t i; for (i = 0; i < m_searchDirectories.size(); i++) { String fullPath = m_searchDirectories[i] + fileName; CharArray fileContents; if (loadFile(fullPath, &fileContents)) { return String(fileContents.ptr()); } } return ""; }
/* We use the garbage collector's feature to "pin" an Object at a * particular memory location to allow C code to write directly into the * contets of a Ruby String (actually, a CharArray, which provides the * storage for String). Since any method on String that mutates self may * cause a new CharArray to be created, we always check whether the * String is pinned and update the RString structure unconditionally. */ void ensure_pinned(NativeMethodEnvironment* env, String* string, RString* rstring) { CharArray* ca = string->data(); size_t byte_size = ca->size(); if(!ca->pinned_p()) { ca = CharArray::create_pinned(env->state(), byte_size); memcpy(ca->raw_bytes(), string->byte_address(), byte_size); string->data(env->state(), ca); } char* ptr = reinterpret_cast<char*>(ca->raw_bytes()); ptr[byte_size-1] = 0; rstring->dmwmb = rstring->ptr = ptr; rstring->len = string->size(); rstring->aux.capa = byte_size; rstring->aux.shared = Qfalse; }
CString Template::ParseCode(LPCTSTR codePtr, LPCTSTR bolPtr, WWhizTemplate* file) { // Build our master string. CharArray buffer; // Remove leading whitespace. while (*codePtr != 0 && *codePtr != '\n' && *codePtr == ' ') { codePtr++; } TokenHelper helper; helper.m_tabSize = 4; helper.m_outBolPtr = bolPtr; helper.m_file = file; helper.m_helper = WNEW TemplateHelper(NULL); // Get the parameter. while (*codePtr != 0) { if (*codePtr == '@' && *(codePtr + 1) == '@') { CString out; if (ParseTag(codePtr, out, &helper)) { // Move the output string into the buffer. for (int i = 0; i < out.GetLength(); i++) buffer.Add(out[i]); } } else { // Copy it straight. buffer.Add(*codePtr++); } } buffer.Add(0); delete helper.m_helper; return CString(buffer.GetData()); }
String* String::append(STATE, const char* other, native_int length) { native_int current_size = size(); native_int data_size = as<CharArray>(data_)->size(); // Clamp the string size the maximum underlying byte array size if(unlikely(current_size > data_size)) { current_size = data_size; } native_int new_size = current_size + length; native_int capacity = data_size; if(capacity < new_size + 1) { // capacity needs one extra byte of room for the trailing null do { // @todo growth should be more intelligent than doubling capacity *= 2; } while(capacity < new_size + 1); // No need to call unshare and duplicate a CharArray // just to throw it away. if(shared_ == Qtrue) shared(state, Qfalse); CharArray* ba = CharArray::create(state, capacity); memcpy(ba->raw_bytes(), byte_address(), current_size); data(state, ba); } else { if(shared_ == Qtrue) unshare(state); } // Append on top of the null byte at the end of s1, not after it memcpy(byte_address() + current_size, other, length); // The 0-based index of the last character is new_size - 1 byte_address()[new_size] = 0; num_bytes(state, Fixnum::from(new_size)); hash_value(state, nil<Fixnum>()); return this; }
value_t rb_glob(String *pattern) { auto array = new (collector) Array; Platform::wrap([&] { std::vector<CharArray> segments; CharArray path = File::normalize_path(pattern->string); path.split([&](const CharArray &part) { segments.push_back(part); }, CharArray("/")); if(File::absolute_path(path)) glob(array, segments, 1, segments[0].size() ? segments[0] : "/"); else glob(array, segments, 0, ""); }); return array; }
VALUE rb_str_resize(VALUE self, size_t len) { NativeMethodEnvironment* env = NativeMethodEnvironment::get(); String* string = capi_get_string(env, self); size_t size = as<CharArray>(string->data())->size(); if(size != len) { if(size < len) { CharArray* ca = CharArray::create_pinned(env->state(), len+1); memcpy(ca->raw_bytes(), string->byte_address(), size); string->data(env->state(), ca); } string->byte_address()[len] = 0; string->num_bytes(env->state(), Fixnum::from(len)); string->hash_value(env->state(), reinterpret_cast<Fixnum*>(RBX_Qnil)); } capi_update_string(env, self); return self; }
void test_compare_bytes() { CharArray* a = String::create(state, "xyZzyx")->data(); CharArray* b = String::create(state, "xyzzyx")->data(); Fixnum* two = Fixnum::from(2); Fixnum* three = Fixnum::from(3); Fixnum* size = Fixnum::from(8); Fixnum* size1 = Fixnum::from(9); TS_ASSERT_EQUALS(a->size(state)->to_native(), 8); TS_ASSERT_EQUALS(b->size(state)->to_native(), 8); TS_ASSERT_EQUALS(a->compare_bytes(state, b, two, two), Fixnum::from(0)); TS_ASSERT_EQUALS(a->compare_bytes(state, b, two, three), Fixnum::from(-1)); TS_ASSERT_EQUALS(a->compare_bytes(state, b, three, two), Fixnum::from(1)); TS_ASSERT_EQUALS(a->compare_bytes(state, b, three, three), Fixnum::from(-1)); TS_ASSERT_EQUALS(a->compare_bytes(state, b, size, size), Fixnum::from(-1)); TS_ASSERT_EQUALS(a->compare_bytes(state, b, size1, size1), Fixnum::from(-1)); }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void LogDestinationFile::log(const LogEvent& logEvent) { String str; bool addLocationInfo = false; Logger::Level logEventLevel = logEvent.level(); if (logEventLevel == Logger::LL_ERROR) { str = logEvent.source() + "**ERROR**: " + logEvent.message(); addLocationInfo = true; } else if (logEventLevel == Logger::LL_WARNING) { str = logEvent.source() + "(warn): " + logEvent.message(); } else if (logEventLevel == Logger::LL_INFO) { str = logEvent.source() + "(i): " + logEvent.message(); } else if (logEventLevel == Logger::LL_DEBUG) { str = logEvent.source() + "(d): " + logEvent.message(); } if (addLocationInfo) { str += "\n"; str += String(" -func: %1\n").arg(logEvent.location().functionName()); str += String(" -file: %1(%2)").arg(logEvent.location().shortFileName()).arg(logEvent.location().lineNumber()); } CharArray charArrMsg = str.toAscii(); const char* szMsg = charArrMsg.ptr(); Mutex::ScopedLock lock(m_mutex); writeToFile(szMsg, true); }