Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: src/api.cc

Issue 16530003: add function to test whether string contents are definitely one byte (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/v8.h ('k') | src/objects.cc » ('j') | src/objects.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 4286 matching lines...) Expand 10 before | Expand all | Expand 10 after
4297 4297
4298 bool String::IsOneByte() const { 4298 bool String::IsOneByte() const {
4299 i::Handle<i::String> str = Utils::OpenHandle(this); 4299 i::Handle<i::String> str = Utils::OpenHandle(this);
4300 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsOneByte()")) { 4300 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsOneByte()")) {
4301 return false; 4301 return false;
4302 } 4302 }
4303 return str->HasOnlyOneByteChars(); 4303 return str->HasOnlyOneByteChars();
4304 } 4304 }
4305 4305
4306 4306
4307 class ContainsOnlyOneByteHelper {
4308 public:
4309 ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
4310 bool Check(i::String* string) {
4311 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
4312 if (cons_string == NULL) return is_one_byte_;
4313 return CheckCons(cons_string);
4314 }
4315 void VisitOneByteString(const uint8_t* chars, int length) {
4316 // Nothing to do.
4317 }
4318 // TODO(dcarney): do word aligned read.
4319 void VisitTwoByteString(const uint16_t* chars, int length) {
4320 // Check whole string without breaking.
4321 uint16_t total = 0;
4322 for (int i = 0; i < length; i++) {
4323 total |= chars[i] >> 8;
4324 }
4325 if (total != 0) is_one_byte_ = false;
4326 }
4327
4328 private:
4329 bool CheckCons(i::ConsString* cons_string) {
4330 while (true) {
4331 // Check left side if flat.
4332 i::String* left = cons_string->first();
4333 i::ConsString* left_as_cons =
4334 i::String::VisitFlat(this, left, 0);
4335 if (!is_one_byte_) return false;
4336 // Check right side if flat.
4337 i::String* right = cons_string->second();
4338 i::ConsString* right_as_cons =
4339 i::String::VisitFlat(this, right, 0);
4340 if (!is_one_byte_) return false;
4341 // Standard recurse/iterate trick.
4342 if (left_as_cons != NULL && right_as_cons != NULL) {
4343 if (left->length() < right->length()) {
4344 CheckCons(left_as_cons);
4345 cons_string = right_as_cons;
4346 } else {
4347 CheckCons(right_as_cons);
4348 cons_string = left_as_cons;
4349 }
4350 // Check fast return.
4351 if (!is_one_byte_) return false;
4352 continue;
4353 }
4354 // Descend left in place.
4355 if (left_as_cons != NULL) {
4356 cons_string = left_as_cons;
4357 continue;
4358 }
4359 // Descend right in place.
4360 if (right_as_cons != NULL) {
4361 cons_string = right_as_cons;
4362 continue;
4363 }
4364 // Terminate.
4365 break;
4366 }
4367 return is_one_byte_;
4368 }
4369 bool is_one_byte_;
4370 DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
4371 };
4372
4373
4374 bool String::ContainsOnlyOneByte() const {
4375 i::Handle<i::String> str = Utils::OpenHandle(this);
4376 if (IsDeadCheck(str->GetIsolate(),
4377 "v8::String::ContainsOnlyOneByte()")) {
4378 return false;
4379 }
4380 if (str->HasOnlyOneByteChars()) return true;
4381 ContainsOnlyOneByteHelper helper;
4382 return helper.Check(*str);
4383 }
4384
4385
4307 class Utf8LengthHelper : public i::AllStatic { 4386 class Utf8LengthHelper : public i::AllStatic {
4308 public: 4387 public:
4309 enum State { 4388 enum State {
4310 kEndsWithLeadingSurrogate = 1 << 0, 4389 kEndsWithLeadingSurrogate = 1 << 0,
4311 kStartsWithTrailingSurrogate = 1 << 1, 4390 kStartsWithTrailingSurrogate = 1 << 1,
4312 kLeftmostEdgeIsCalculated = 1 << 2, 4391 kLeftmostEdgeIsCalculated = 1 << 2,
4313 kRightmostEdgeIsCalculated = 1 << 3, 4392 kRightmostEdgeIsCalculated = 1 << 3,
4314 kLeftmostEdgeIsSurrogate = 1 << 4, 4393 kLeftmostEdgeIsSurrogate = 1 << 4,
4315 kRightmostEdgeIsSurrogate = 1 << 5 4394 kRightmostEdgeIsSurrogate = 1 << 5
4316 }; 4395 };
(...skipping 3538 matching lines...) Expand 10 before | Expand all | Expand 10 after
7855 7934
7856 v->VisitPointers(blocks_.first(), first_block_limit_); 7935 v->VisitPointers(blocks_.first(), first_block_limit_);
7857 7936
7858 for (int i = 1; i < blocks_.length(); i++) { 7937 for (int i = 1; i < blocks_.length(); i++) {
7859 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); 7938 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
7860 } 7939 }
7861 } 7940 }
7862 7941
7863 7942
7864 } } // namespace v8::internal 7943 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/objects.cc » ('j') | src/objects.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698