1 // Copyright 2013 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 // limitations under the License. 28 29 #ifndef V8_EXTENSIONS_I18N_SRC_UTILS_H_ 30 #define V8_EXTENSIONS_I18N_SRC_UTILS_H_ 31 32 #include "unicode/uversion.h" 33 #include "v8.h" 34 35 namespace U_ICU_NAMESPACE { 36 class UnicodeString; 37 } 38 39 namespace v8_i18n { 40 41 class Utils { 42 public: 43 // Safe string copy. Null terminates the destination. Copies at most 44 // (length - 1) bytes. 45 // We can't use snprintf since it's not supported on all relevant platforms. 46 // We can't use OS::SNPrintF, it's only for internal code. 47 static void StrNCopy(char* dest, int length, const char* src); 48 49 // Converts v8::String into UnicodeString. Returns false if input 50 // can't be converted into utf8. 51 static bool V8StringToUnicodeString(const v8::Handle<v8::Value>& input, 52 icu::UnicodeString* output); 53 54 // Extract a String setting named in |settings| and set it to |result|. 55 // Return true if it's specified. Otherwise, return false. 56 static bool ExtractStringSetting(const v8::Handle<v8::Object>& settings, 57 const char* setting, 58 icu::UnicodeString* result); 59 60 // Extract a Integer setting named in |settings| and set it to |result|. 61 // Return true if it's specified. Otherwise, return false. 62 static bool ExtractIntegerSetting(const v8::Handle<v8::Object>& settings, 63 const char* setting, 64 int32_t* result); 65 66 // Extract a Boolean setting named in |settings| and set it to |result|. 67 // Return true if it's specified. Otherwise, return false. 68 static bool ExtractBooleanSetting(const v8::Handle<v8::Object>& settings, 69 const char* setting, 70 bool* result); 71 72 // Converts ASCII array into UChar array. 73 // Target is always \0 terminated. 74 static void AsciiToUChar(const char* source, 75 int32_t source_length, 76 UChar* target, 77 int32_t target_length); 78 79 // Creates an ObjectTemplate with one internal field. 80 static v8::Local<v8::ObjectTemplate> GetTemplate(v8::Isolate* isolate); 81 82 // Creates an ObjectTemplate with two internal fields. 83 static v8::Local<v8::ObjectTemplate> GetTemplate2(v8::Isolate* isolate); 84 85 private: 86 Utils() {} 87 }; 88 89 } // namespace v8_i18n 90 91 #endif // V8_EXTENSIONS_I18N_UTILS_H_ 92