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

Side by Side Diff: src/extensions/experimental/break-iterator.cc

Issue 6598014: Adding break iterator support to the i18n api extension.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 9 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "break-iterator.h"
29
30 #include "unicode/brkiter.h"
31 #include "unicode/locid.h"
32 #include "unicode/uloc.h"
33 #include "unicode/rbbi.h"
34
35 namespace v8 {
36 namespace internal {
37
38 v8::Persistent<v8::ObjectTemplate> BreakIterator::break_iterator_template_;
39
40 // Unpacks break iterator object from corresponding JavaScript object.
41 static icu::BreakIterator* UnpackBreakIterator(v8::Handle<v8::Object> obj) {
42 return static_cast<icu::BreakIterator*>(obj->GetPointerFromInternalField(0));
43 }
44
45 // Release memory we allocated for the BreakIterator once the JS object that
46 // holds the pointer gets garbage collected.
47 static void DeleteBreakIterator(v8::Persistent<v8::Value> object,
48 void* param) {
49 delete UnpackBreakIterator(v8::Persistent<v8::Object>::Cast(object));
50 }
51
52 // Assigns new text to the iterator.
53 static v8::Handle<v8::Value> BreakIteratorAssign(const v8::Arguments& args) {
54 if (args.Length() != 1 || !args[0]->IsString()) {
55 return v8::ThrowException(v8::Exception::SyntaxError(
56 v8::String::New("Text input is required.")));
57 }
58
59 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
60
61 v8::Local<v8::String> text_value = args[0]->ToString();
62 UnicodeString text(*v8::String::Utf8Value(text_value),
63 text_value->Utf8Length());
jungshik at Google 2011/02/26 00:31:38 Chrome's ICU didn't define a macro (U_CHARSET_IS_U
Nebojša Ćirić 2011/02/26 01:13:17 Done.
64
65 break_iterator->setText(text);
66
67 return v8::Undefined();
68 }
69
70 // Moves iterator to the beginning of the string and returns new position.
71 static v8::Handle<v8::Value> BreakIteratorFirst(const v8::Arguments& args) {
72 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
73
74 return v8::Int32::New(break_iterator->first());
75 }
76
77 // Moves iterator to the next position and returns it.
78 static v8::Handle<v8::Value> BreakIteratorNext(const v8::Arguments& args) {
79 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
80
81 return v8::Int32::New(break_iterator->next());
jungshik at Google 2011/02/26 00:31:38 When there's no more item, ICU's next() will retur
Nebojša Ćirić 2011/02/26 00:47:45 UBRK_DONE is actually int32_t and is represented a
82 }
83
84 // Returns current iterator's current position.
85 static v8::Handle<v8::Value> BreakIteratorCurrent(const v8::Arguments& args) {
86 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
87
88 return v8::Int32::New(break_iterator->current());
89 }
90
91 // Returns type of the item from current position.
92 // This call is only valid for word break iterators. Others just return 0.
93 static v8::Handle<v8::Value> BreakIteratorBreakType(
94 const v8::Arguments& args) {
95 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
96
97 // TODO(cira): Remove cast once ICU fixes base BreakIterator class.
98 int32_t status =
99 static_cast<RuleBasedBreakIterator*>(break_iterator)->getRuleStatus();
100 // Keep return values in sync with JavaScript BreakType enum.
101 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) {
102 return v8::Int32::New(UBRK_WORD_NONE);
103 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) {
104 return v8::Int32::New(UBRK_WORD_NUMBER);
105 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) {
106 return v8::Int32::New(UBRK_WORD_LETTER);
107 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) {
108 return v8::Int32::New(UBRK_WORD_KANA);
109 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) {
110 return v8::Int32::New(UBRK_WORD_IDEO);
jungshik at Google 2011/02/26 00:31:38 We need to give a bit more thought on what types t
Nebojša Ćirić 2011/02/26 00:47:45 We can take them out at any time - it's still expe
111 } else {
112 return v8::Int32::New(-1);
113 }
114 }
115
116 v8::Handle<v8::Value> BreakIterator::JSBreakIterator(
117 const v8::Arguments& args) {
118 // No need to check args here, we do it in JavaScript.
119
120 const char* locale = *v8::String::Utf8Value(args[0]->ToString());
121 icu::Locale icu_locale(locale);
122
123 UErrorCode status = U_ZERO_ERROR;
124 icu::BreakIterator* break_iterator = NULL;
125 const char* type = *v8::String::Utf8Value(args[1]->ToString());
126 if (!strcmp(type, "character")) {
127 break_iterator =
128 icu::BreakIterator::createCharacterInstance(icu_locale, status);
129 } else if (!strcmp(type, "word")) {
130 break_iterator =
131 icu::BreakIterator::createWordInstance(icu_locale, status);
132 } else if (!strcmp(type, "sentence")) {
133 break_iterator =
134 icu::BreakIterator::createSentenceInstance(icu_locale, status);
135 } else if (!strcmp(type, "line")) {
136 break_iterator =
137 icu::BreakIterator::createLineInstance(icu_locale, status);
138 } else {
139 return v8::ThrowException(v8::Exception::SyntaxError(
140 v8::String::New("Invalid iterator type.")));
141 }
142
143 if (U_FAILURE(status)) {
144 delete break_iterator;
145 return v8::ThrowException(v8::Exception::Error(
146 v8::String::New("Failed to create break iterator.")));
147 }
148
149 v8::HandleScope handle_scope;
150
151 if (break_iterator_template_.IsEmpty()) {
152 v8::Handle<v8::ObjectTemplate> raw_template(v8::ObjectTemplate::New());
153 raw_template->SetInternalFieldCount(1);
154 raw_template->Set(v8::String::New("assign"),
jungshik at Google 2011/02/26 00:31:38 How about 'adopt' or 'adoptText'? 'assign' is kind
Nebojša Ćirić 2011/02/26 00:47:45 I'll fix that. On 2011/02/26 00:31:38, Jungshik S
Nebojša Ćirić 2011/02/26 01:13:17 Done.
155 v8::FunctionTemplate::New(BreakIteratorAssign));
156 raw_template->Set(v8::String::New("first"),
157 v8::FunctionTemplate::New(BreakIteratorFirst));
158 raw_template->Set(v8::String::New("next"),
159 v8::FunctionTemplate::New(BreakIteratorNext));
160 raw_template->Set(v8::String::New("current"),
161 v8::FunctionTemplate::New(BreakIteratorCurrent));
162 raw_template->Set(v8::String::New("breakType"),
163 v8::FunctionTemplate::New(BreakIteratorBreakType));
jungshik at Google 2011/02/26 00:31:38 One method is missing; a method that returns the
Nebojša Ćirić 2011/02/26 00:47:45 JavaScript has two (actually 3, but one is depreca
164 break_iterator_template_ =
165 v8::Persistent<v8::ObjectTemplate>::New(raw_template);
166 // Make template weak so we can delete iterator once GC kicks in.
167 break_iterator_template_.MakeWeak(NULL, DeleteBreakIterator);
168 }
169
170 //Create an empty object wrapper.
171 v8::Handle<v8::Object> wrapper = break_iterator_template_->NewInstance();
172 // Set break iterator as internal field of the resulting JS object.
173 wrapper->SetPointerInInternalField(0, break_iterator);
174
175 return handle_scope.Close(wrapper);
176 }
177
178 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/experimental/break-iterator.h ('k') | src/extensions/experimental/experimental.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698