OLD | NEW |
---|---|
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 4066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4077 } else { | 4077 } else { |
4078 // If we didn't actually change anything in doing the conversion | 4078 // If we didn't actually change anything in doing the conversion |
4079 // we simple return the result and let the converted string | 4079 // we simple return the result and let the converted string |
4080 // become garbage; there is no reason to keep two identical strings | 4080 // become garbage; there is no reason to keep two identical strings |
4081 // alive. | 4081 // alive. |
4082 return s; | 4082 return s; |
4083 } | 4083 } |
4084 } | 4084 } |
4085 | 4085 |
4086 | 4086 |
4087 template <class Converter> | 4087 static inline SeqAsciiString* TryGetSeqAsciiString(String* s) { |
4088 static Object* ConvertCase(Arguments args, | 4088 if (!s->IsFlat() || !s->IsAsciiRepresentation()) return NULL; |
4089 unibrow::Mapping<Converter, 128>* mapping) { | 4089 if (s->IsConsString()) { |
4090 ASSERT(ConsString::cast(s)->second()->length() == 0); | |
4091 return SeqAsciiString::cast(ConsString::cast(s)->first()); | |
4092 } | |
4093 return SeqAsciiString::cast(s); | |
4094 } | |
4095 | |
4096 | |
4097 namespace { | |
4098 | |
4099 struct ToLowerTraits { | |
4100 typedef unibrow::ToLowercase UnibrowConverter; | |
4101 | |
4102 static bool ConvertAscii(char* dst, char* src, int length) { | |
antonm
2010/03/03 16:20:11
just curious if this and below loops could be sped
Vitaly Repeshko
2010/03/03 16:40:46
Performance tuning is poisonous :)
GCC 4.2.4 with
| |
4103 bool changed = false; | |
4104 for (int i = 0; i < length; ++i) { | |
4105 char c = src[i]; | |
4106 if ('A' <= c && c <= 'Z') { | |
4107 c += ('a' - 'A'); | |
4108 changed = true; | |
4109 } | |
4110 dst[i] = c; | |
4111 } | |
4112 return changed; | |
4113 } | |
4114 }; | |
4115 | |
4116 | |
4117 struct ToUpperTraits { | |
4118 typedef unibrow::ToUppercase UnibrowConverter; | |
4119 | |
4120 static bool ConvertAscii(char* dst, char* src, int length) { | |
4121 bool changed = false; | |
4122 for (int i = 0; i < length; ++i) { | |
4123 char c = src[i]; | |
4124 if ('a' <= c && c <= 'z') { | |
4125 c -= ('a' - 'A'); | |
4126 changed = true; | |
4127 } | |
4128 dst[i] = c; | |
4129 } | |
4130 return changed; | |
4131 } | |
4132 }; | |
4133 | |
4134 } // namespace | |
4135 | |
4136 | |
4137 template <typename ConvertTraits> | |
4138 static Object* ConvertCase( | |
4139 Arguments args, | |
4140 unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { | |
4090 NoHandleAllocation ha; | 4141 NoHandleAllocation ha; |
4091 | |
4092 CONVERT_CHECKED(String, s, args[0]); | 4142 CONVERT_CHECKED(String, s, args[0]); |
4093 s->TryFlatten(); | 4143 s->TryFlatten(); |
4094 | 4144 |
4095 int input_string_length = s->length(); | 4145 const int length = s->length(); |
4096 // Assume that the string is not empty; we need this assumption later | 4146 // Assume that the string is not empty; we need this assumption later |
4097 if (input_string_length == 0) return s; | 4147 if (length == 0) return s; |
4098 int length = input_string_length; | 4148 |
4149 // Simpler handling of ascii strings. | |
4150 // | |
4151 // NOTE: This assumes that the upper/lower case of an ascii | |
4152 // character is also ascii. This is currently the case, but it | |
4153 // might break in the future if we implement more context and locale | |
4154 // dependent upper/lower conversions. | |
4155 SeqAsciiString* seq_ascii = TryGetSeqAsciiString(s); | |
4156 if (seq_ascii != NULL) { | |
4157 Object* o = Heap::AllocateRawAsciiString(length); | |
4158 if (o->IsFailure()) return o; | |
4159 SeqAsciiString* result = SeqAsciiString::cast(o); | |
4160 bool has_changed_character = ConvertTraits::ConvertAscii( | |
4161 result->GetChars(), seq_ascii->GetChars(), length); | |
4162 return has_changed_character ? result : s; | |
4163 } | |
4099 | 4164 |
4100 Object* answer = ConvertCaseHelper(s, length, length, mapping); | 4165 Object* answer = ConvertCaseHelper(s, length, length, mapping); |
4101 if (answer->IsSmi()) { | 4166 if (answer->IsSmi()) { |
4102 // Retry with correct length. | 4167 // Retry with correct length. |
4103 answer = ConvertCaseHelper(s, Smi::cast(answer)->value(), length, mapping); | 4168 answer = ConvertCaseHelper(s, Smi::cast(answer)->value(), length, mapping); |
4104 } | 4169 } |
4105 return answer; // This may be a failure. | 4170 return answer; // This may be a failure. |
4106 } | 4171 } |
4107 | 4172 |
4108 | 4173 |
4109 static Object* Runtime_StringToLowerCase(Arguments args) { | 4174 static Object* Runtime_StringToLowerCase(Arguments args) { |
4110 return ConvertCase<unibrow::ToLowercase>(args, &to_lower_mapping); | 4175 return ConvertCase<ToLowerTraits>(args, &to_lower_mapping); |
4111 } | 4176 } |
4112 | 4177 |
4113 | 4178 |
4114 static Object* Runtime_StringToUpperCase(Arguments args) { | 4179 static Object* Runtime_StringToUpperCase(Arguments args) { |
4115 return ConvertCase<unibrow::ToUppercase>(args, &to_upper_mapping); | 4180 return ConvertCase<ToUpperTraits>(args, &to_upper_mapping); |
4116 } | 4181 } |
4117 | 4182 |
4183 | |
4118 static inline bool IsTrimWhiteSpace(unibrow::uchar c) { | 4184 static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
4119 return unibrow::WhiteSpace::Is(c) || c == 0x200b; | 4185 return unibrow::WhiteSpace::Is(c) || c == 0x200b; |
4120 } | 4186 } |
4121 | 4187 |
4188 | |
4122 static Object* Runtime_StringTrim(Arguments args) { | 4189 static Object* Runtime_StringTrim(Arguments args) { |
4123 NoHandleAllocation ha; | 4190 NoHandleAllocation ha; |
4124 ASSERT(args.length() == 3); | 4191 ASSERT(args.length() == 3); |
4125 | 4192 |
4126 CONVERT_CHECKED(String, s, args[0]); | 4193 CONVERT_CHECKED(String, s, args[0]); |
4127 CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); | 4194 CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
4128 CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); | 4195 CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
4129 | 4196 |
4130 s->TryFlatten(); | 4197 s->TryFlatten(); |
4131 int length = s->length(); | 4198 int length = s->length(); |
(...skipping 4283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8415 } else { | 8482 } else { |
8416 // Handle last resort GC and make sure to allow future allocations | 8483 // Handle last resort GC and make sure to allow future allocations |
8417 // to grow the heap without causing GCs (if possible). | 8484 // to grow the heap without causing GCs (if possible). |
8418 Counters::gc_last_resort_from_js.Increment(); | 8485 Counters::gc_last_resort_from_js.Increment(); |
8419 Heap::CollectAllGarbage(false); | 8486 Heap::CollectAllGarbage(false); |
8420 } | 8487 } |
8421 } | 8488 } |
8422 | 8489 |
8423 | 8490 |
8424 } } // namespace v8::internal | 8491 } } // namespace v8::internal |
OLD | NEW |