Stdlib: Removed all references to String constructors in nit stdlib and tests.
authorLucas Bajolet <r4pass@hotmail.com>
Thu, 29 Aug 2013 21:23:14 +0000 (17:23 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 29 Aug 2013 21:23:14 +0000 (17:23 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

39 files changed:
contrib/sort_downloads/src/sort_downloads.nit
lib/base64.nit
lib/curl/curl_c.nit
lib/gtk3_4/gtk_assistant.nit
lib/gtk3_4/gtk_core.nit
lib/gtk3_4/gtk_dialogs.nit
lib/gtk3_4/gtk_widgets_ext.nit
lib/json/json_reader.nit
lib/json/json_writer.nit
lib/md5/md5.nit
lib/md5/md5.nit.c
lib/socket/socket_c.nit
lib/sqlite3/sqlite3.nit
lib/standard/environ.nit
lib/standard/file.nit
lib/standard/file_nit.c
lib/standard/string.nit
lib/standard/time.nit
tests/sav/error_needed_method_alt3.res
tests/sav/error_needed_method_alt3.sav
tests/string_ffi_ref_test.nit
tests/test_ffi_c_accessor.nit
tests/test_ffi_c_callback_extern_receiver.nit
tests/test_ffi_c_more.nit
tests/test_ffi_c_strings.nit
tests/test_ffi_c_super.nit
tests/test_ni_accessor.nit
tests/test_ni_accessor.nit.c
tests/test_ni_inits.nit.c
tests/test_ni_null.nit
tests/test_ni_null.nit.c
tests/test_ni_pointer.nit
tests/test_ni_pointer.nit.c
tests/test_ni_special_pointer.nit
tests/test_ni_special_pointer.nit.c
tests/test_ni_strings.nit
tests/test_ni_strings.nit.c
tests/test_ni_super.nit
tests/test_ni_super.nit.c

index 9424ee6..ba96d5d 100755 (executable)
@@ -63,10 +63,10 @@ redef class String
 
        # Returns null on success
        fun file_rename_to(dest: String): nullable String import String::to_cstring,
-       String::from_cstring, String as nullable `{
+       NativeString::to_s, String as nullable `{
                int res = rename(String_to_cstring(recv), String_to_cstring(dest));
                if (res == 0) return null_String();
-               return String_as_nullable(new_String_from_cstring(strerror(errno)));
+               return String_as_nullable(NativeString_to_s(strerror(errno)));
        `}
 
        # Replace `~` by the path to the home diretory
index 8a718eb..861cb9a 100644 (file)
@@ -72,7 +72,7 @@ redef class String
                        end
                end
 
-               return new String.from_cstring( result )
+               return result.to_s
        end
 
        # Decodes the receiver string from base64.
@@ -131,6 +131,6 @@ redef class String
                        result[s*3] = e.rshift(2*8).bin_and( mask_8bit ).ascii
                end
 
-               return new String.from_cstring( result )
+               return result.to_s
        end
 end
index 61525a6..c5dbf85 100644 (file)
@@ -48,17 +48,17 @@ in "C body" `{
        size_t nit_curl_callback_func(void *buffer, size_t size, size_t count, CURLCallbackDatas *datas){
                if(datas->type == CURLcallbackTypeHeader){
                        char *line_c = (char*)buffer;
-                       String line_o = new_String_copy_from_native(line_c);
+                       String line_o = NativeString_to_s_with_copy(line_c);
                        CCurlCallbacks_header_callback(datas->delegate, line_o);
                }
                else if(datas->type == CURLcallbackTypeBody){
                        char *line_c = (char*)buffer;
-                       String line_o = new_String_copy_from_native(line_c);
+                       String line_o = NativeString_to_s_with_copy(line_c);
                        CCurlCallbacks_body_callback(datas->delegate, line_o);
                }
                else if(datas->type == CURLcallbackTypeStream){
                        char *line_c = (char*)buffer;
-                       String line_o = new_String_from_cstring(line_c);
+                       String line_o = NativeString_to_s(line_c);
                        CCurlCallbacks_stream_callback(datas->delegate, line_o, size, count);
                }
                return count;
@@ -113,11 +113,11 @@ extern CCurl `{ CURL * `}
                 return answ
        end
        # Internal method used to get String object information initially knowns as C Chars type
-       private fun i_getinfo_chars(opt: CURLInfoChars, res: CURLInfoResponseString):CURLCode import CURLInfoResponseString::response= `{
+       private fun i_getinfo_chars(opt: CURLInfoChars, res: CURLInfoResponseString):CURLCode import CURLInfoResponseString::response=, NativeString::to_s_with_copy `{
                char *r = NULL;
                CURLcode c = curl_easy_getinfo( recv, opt, &r);
                if((c == CURLE_OK) && r != NULL){
-                       String ro = new_String_copy_from_native(r);
+                       String ro = NativeString_to_s_with_copy(r);
                        CURLInfoResponseString_response__assign( res, ro);
                }
                return c;
@@ -185,14 +185,13 @@ extern CCurl `{ CURL * `}
                return once new CURLCode.unknown_option
        end
        # Internal method used to configure read callback
-       private fun i_register_read_datas_callback(delegate: CCurlCallbacks, datas: String, size: Int):CURLCode import String::to_cstring, String::copy_from_native `{
+       private fun i_register_read_datas_callback(delegate: CCurlCallbacks, datas: String, size: Int):CURLCode import String::to_cstring `{
                CURLCallbackReadDatas *d = NULL;
                d = malloc(sizeof(CURLCallbackReadDatas));
                d->data = (char*)String_to_cstring(datas);
                d->len = size;
                d->pos = 0;
                return curl_easy_setopt( recv, CURLOPT_READDATA, d);
-
        `}
        # Internal method used to configure callbacks in terms of given type
        private fun i_register_callback(delegate: CCurlCallbacks, cbtype: CURLCallbackType):CURLCode is extern import CCurlCallbacks::header_callback,  CCurlCallbacks::body_callback, CCurlCallbacks::stream_callback  `{
@@ -221,11 +220,11 @@ extern CCurl `{ CURL * `}
                return e;
        `}
        # Convert given string to URL encoded string
-       fun escape(url: String):String `{
+       fun escape(url: String):String import String::to_cstring, NativeString::to_s_with_copy `{
                char *orig_url, *encoded_url = NULL;
                orig_url = String_to_cstring(url);
                encoded_url = curl_easy_escape( recv, orig_url, strlen(orig_url));
-               String b_url = new_String_copy_from_native(encoded_url);
+               String b_url = NativeString_to_s_with_copy(encoded_url);
                curl_free(encoded_url);
                return b_url;
        `}
@@ -282,9 +281,9 @@ extern CURLCode `{ CURLcode `}
        fun is_valid_protocol:Bool `{ return recv == CURLE_UNSUPPORTED_PROTOCOL; `}
        fun is_valid_init:Bool `{ return recv == CURLE_FAILED_INIT; `}
        fun to_i:Int do return code end
-       redef fun to_s `{
+       redef fun to_s import NativeString::to_s_with_copy `{
                char *c = (char*)curl_easy_strerror(recv);
-               return new_String_copy_from_native(c);
+               return NativeString_to_s_with_copy(c);
        `}
 end
 
@@ -310,7 +309,7 @@ extern CURLSList `{ struct curl_slist * `}
        # Internal method to check for reachability of next element
        private fun i_next_reachable(c: CURLSList):Bool `{ return (c != NULL && c->next != NULL); `}
        # Internal method to get current data
-       private fun i_data(c: CURLSList):String `{ return new_String_from_cstring(c->data); `}
+       private fun i_data(c: CURLSList):String `{ return NativeString_to_s(c->data); `}
        # Internal method to get next element
        private fun i_next(c: CURLSList):CURLSList `{ return c->next; `}
        # Convert current low level List to an Array[String] object
index 71d7246..282e35a 100644 (file)
@@ -72,7 +72,7 @@ extern GtkAssistant `{GtkAssistant *`}
        `}
 
        fun get_page_title( page : GtkWidget ) : String is extern `{
-               return new_String_from_cstring( (char *)gtk_assistant_get_page_title( recv, page ) );
+               return NativeString_to_s( (char *)gtk_assistant_get_page_title( recv, page ) );
        `}
 
        fun set_page_title( page : GtkWidget, title : String) is extern import String::to_cstring`{
index 3a8d2b8..f35a1d2 100644 (file)
@@ -248,7 +248,7 @@ extern GtkFrame `{GtkFrame *`}
        `}
 
        fun frame_label : String is extern`{
-               return new_String_from_cstring( (char *)gtk_frame_get_label( recv ) );
+               return NativeString_to_s( (char *)gtk_frame_get_label( recv ) );
        `}
 
        fun frame_label=( lbl : String ) is extern import String::to_cstring`{
@@ -334,7 +334,7 @@ extern GtkEntry `{GtkEntry *`}
        `}
 
        fun text : String is extern import String::to_cstring`{
-               return new_String_from_cstring( (char *)gtk_entry_get_text( recv ) );
+               return NativeString_to_s( (char *)gtk_entry_get_text( recv ) );
        `}
 
        fun text=( value : String) is extern import String::to_cstring`{
@@ -540,8 +540,8 @@ extern GtkLabel `{GtkLabel *`}
        `}
 
        # Returns the text of the label
-       fun text : String import String::from_cstring `{
-               return new_String_from_cstring( (char*)gtk_label_get_text( recv ) );
+       fun text : String import NativeString::to_s `{
+               return NativeString_to_s( (char*)gtk_label_get_text( recv ) );
        `}
 
        # Sets the angle of rotation for the label.
@@ -644,7 +644,7 @@ extern GtkButton `{GtkButton *`}
        `}
 
        fun text : String is extern `{
-               return new_String_from_cstring( (char *)gtk_button_get_label( recv ) );
+               return NativeString_to_s( (char *)gtk_button_get_label( recv ) );
        `}
 
        fun text=( value : String ) is extern import String::to_cstring`{
@@ -708,7 +708,7 @@ extern GtkExpander `{GtkExpander *`}
        `}
 
        fun label_text : String is extern `{
-               return new_String_from_cstring( (char *)gtk_expander_get_label( recv ) );
+               return NativeString_to_s( (char *)gtk_expander_get_label( recv ) );
        `}
 
        fun label_text=( lbl : String ) is extern import String::to_cstring`{
@@ -833,7 +833,7 @@ extern GtkComboBox `{GtkComboBox *`}
        `}
 
        fun active_id : String is extern `{
-               return new_String_from_cstring( (char *)gtk_combo_box_get_active_id( recv ) );
+               return NativeString_to_s( (char *)gtk_combo_box_get_active_id( recv ) );
        `}
 
        fun active_id=( id_active : String ) is extern import String::to_cstring`{
@@ -857,7 +857,7 @@ extern GtkComboBox `{GtkComboBox *`}
        `}
 
        fun title : String is extern`{
-               return new_String_from_cstring( (char *)gtk_combo_box_get_title( recv ) );
+               return NativeString_to_s( (char *)gtk_combo_box_get_title( recv ) );
        `}
 
        fun title=( t : String ) is extern import String::to_cstring`{
index 79d38b8..17e6c94 100644 (file)
@@ -51,7 +51,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun program_name : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_program_name( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_program_name( recv ) );
        `}
 
        fun program_name=( name : String ) is extern import String::to_cstring`{
@@ -59,7 +59,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun version : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_version( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_version( recv ) );
        `}
 
        fun version=( v : String ) is extern import String::to_cstring`{
@@ -67,7 +67,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun copyright : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_copyright( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_copyright( recv ) );
        `}
 
        fun copyright=( c : String ) is extern import String::to_cstring`{
@@ -75,7 +75,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun comments : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_comments( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_comments( recv ) );
        `}
 
        fun comments=( com : String ) is extern import String::to_cstring`{
@@ -83,7 +83,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun license : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_license( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_license( recv ) );
        `}
 
        fun license=( li : String ) is extern import String::to_cstring`{
@@ -93,7 +93,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        #license_type
 
        fun website : String is extern`{
-               return new_String_from_cstring( (char *)gtk_about_dialog_get_website( recv ) );
+               return NativeString_to_s( (char *)gtk_about_dialog_get_website( recv ) );
        `}
 
        fun website=( link : String ) is extern import String::to_cstring`{
@@ -101,7 +101,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
        `}
 
        fun website_label : String is extern`{
-               return new_String_from_cstring( (char *) gtk_about_dialog_get_website_label( recv ) );
+               return NativeString_to_s( (char *) gtk_about_dialog_get_website_label( recv ) );
        `}
 
        fun website_label=( link_label : String ) is extern import String::to_cstring`{
@@ -110,7 +110,7 @@ extern GtkAboutDialog `{GtkAboutDialog *`}
 
        #TODO
        #fun authors : String is extern`{
-       #               return new_String_from_cstring( gtk_about_dialog_get_authors( recv ) );
+       #               return NativeString_to_s( gtk_about_dialog_get_authors( recv ) );
        #`}
 
        #TODO
@@ -140,7 +140,7 @@ extern GtkAppChooserDialog `{GtkAppChooserDialog *`}
        fun widget : GtkWidget is extern `{ return gtk_app_chooser_dialog_get_widget( recv ); `}
 
        fun heading : String is extern `{
-               return new_String_from_cstring( (char *)gtk_app_chooser_dialog_get_heading( recv ) );
+               return NativeString_to_s( (char *)gtk_app_chooser_dialog_get_heading( recv ) );
        `}
 
        fun heading=( text : String ) is extern import String::to_cstring `{
index 03b1f66..c0ea16d 100644 (file)
@@ -131,7 +131,7 @@ extern GtkProgressBar `{GtkProgressBar *`}
        `}
 
        fun text : String is extern import String::to_cstring`{
-               return new_String_from_cstring( (char *)gtk_progress_bar_get_text( recv ) );
+               return NativeString_to_s( (char *)gtk_progress_bar_get_text( recv ) );
        `}
 
        fun text=( value : String) is extern import String::to_cstring`{
index b99bb39..f412c87 100644 (file)
@@ -18,7 +18,7 @@ intrude import jsonable
 redef class String
        # Deserializes this String and return its value as a Map[String, nullable Jsonable]
        # On error, null is returned.
-       fun json_to_object : nullable Map[String, nullable Jsonable] import String::from_cstring, JsonObject::json_to_map `{
+       fun json_to_object : nullable Map[String, nullable Jsonable] import NativeString::to_s, JsonObject::json_to_map `{
                char *native_recv;
                json_object *jobj;
                nullable_Map map;
@@ -34,7 +34,7 @@ end
 
 redef extern JsonObject
        # Get this json object as a Map
-       private fun json_to_map : nullable Map[String, nullable Jsonable] import String::from_cstring, String::to_cstring, HashMap, HashMap::[]=, json_cross, HashMap[String,nullable Jsonable] as( nullable Map[String,nullable Jsonable] ), String as ( Object ), nullable Jsonable as (nullable Object) `{
+       private fun json_to_map : nullable Map[String, nullable Jsonable] import NativeString::to_s, String::to_cstring, HashMap, HashMap::[]=, json_cross, HashMap[String,nullable Jsonable] as( nullable Map[String,nullable Jsonable] ), String as ( Object ), nullable Jsonable as (nullable Object) `{
                HashMap map;
                String nit_key;
                nullable_Jsonable nit_val;
@@ -44,7 +44,7 @@ redef extern JsonObject
 
                { /* prevents "mixed declaration and code" warning for C90 */
                json_object_object_foreach( recv, key, val ) {
-                       nit_key = new_String_from_cstring( key );
+                       nit_key = NativeString_to_s( key );
 
                        if ( val == NULL ) type = json_type_null;
                        else type = json_object_get_type( val );
@@ -98,10 +98,10 @@ redef extern JsonObject
        `}
 
        # Get this json object as a String
-       private fun json_to_string : String import String::from_cstring `{
+       private fun json_to_string : String import NativeString::to_s `{
                const char *cstring;
                cstring = json_object_get_string( recv );
-               return new_String_from_cstring( (char*)cstring );
+               return NativeString_to_s( (char*)cstring );
        `}
 
        # Intermediate function to convert to gt this Json object as a given type.
index 01dc603..5e5cef2 100644 (file)
@@ -40,7 +40,7 @@ redef class Map[ K, V ]
 #else
                json_native_string = json_object_to_json_string( jobj );
 #endif
-               json_string = new_String_from_cstring( (char*)json_native_string );
+               json_string = NativeString_to_s( (char*)json_native_string );
                return json_string;
        `}
 
@@ -93,7 +93,7 @@ redef class Sequence[ E ]
 end
 
 redef class String
-       redef fun to_json_object import String::from_cstring `{
+       redef fun to_json_object import NativeString::to_s `{
                char *native_recv = String_to_cstring( recv );
                return json_object_new_string( native_recv );
        `}
index 509358f..1f474e0 100644 (file)
@@ -20,5 +20,5 @@ module md5
 redef class String
        # returns the md5 digest of the receiver string
        # algorithm implemented by L. Peter Deutsch <ghost@aladdin.com>
-       fun md5 : String is extern import String::to_cstring, String::from_cstring, String::print
+       fun md5 : String is extern import String::to_cstring, NativeString::to_s, String::print
 end
index bd05193..0a26386 100644 (file)
@@ -18,7 +18,7 @@ C implementation of md5::String::md5_digest
 
 Imported methods signatures:
        char * String_to_cstring( String recv ) for string::String::to_cstring
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String String_md5___impl( String recv )
 {
@@ -38,7 +38,7 @@ String String_md5___impl( String recv )
                sprintf(hex_output + di * 2, "%02x", digest[di]);
        hex_output[ 32 ] = '\0';
 
-       return new_String_from_cstring( hex_output );
+       return NativeString_to_s( hex_output );
 }
 
 
index 4843354..f312fe1 100644 (file)
@@ -123,7 +123,7 @@ extern FFSocket `{ S_DESCRIPTOR* `}
                int n = read(*recv, c, (sizeof(c)-1));
                if(n < 0) exit(-1);
                c[n] = '\0';
-               return new_String_from_cstring(c);
+               return NativeString_to_s(c);
        `}
 
        fun bind(addrIn: FFSocketAddrIn): Int `{ return bind(*recv, (S_ADDR*)addrIn, sizeof(*addrIn)); `}
@@ -207,14 +207,14 @@ extern FFSocketAddrIn `{ S_ADDR_IN* `}
                memcpy( (char*)&sai->sin_addr.s_addr, (char*)hostent->h_addr, hostent->h_length );
                return sai;
        `}
-       fun address: String `{ return new_String_from_cstring( (char*)inet_ntoa(recv->sin_addr) ); `}
+       fun address: String `{ return NativeString_to_s( (char*)inet_ntoa(recv->sin_addr) ); `}
        fun family: FFSocketAddressFamilies `{ return recv->sin_family; `}
        fun port: Int `{ return ntohs(recv->sin_port); `}
        fun destroy `{ free(recv); `}
 end
 
 extern FFSocketHostent `{ S_HOSTENT* `}
-       private fun i_h_aliases(i: Int): String `{ return new_String_from_cstring(recv->h_aliases[i]); `}
+       private fun i_h_aliases(i: Int): String `{ return NativeString_to_s(recv->h_aliases[i]); `}
        private fun i_h_aliases_reachable(i: Int): Bool `{ return (recv->h_aliases[i] != NULL); `}
        fun h_aliases: Array[String]
        do
@@ -227,10 +227,10 @@ extern FFSocketHostent `{ S_HOSTENT* `}
                end
                return d
        end
-       fun h_addr: String `{ return new_String_from_cstring( (char*)inet_ntoa(*(S_IN_ADDR*)recv->h_addr) ); `}
+       fun h_addr: String `{ return NativeString_to_s( (char*)inet_ntoa(*(S_IN_ADDR*)recv->h_addr) ); `}
        fun h_addrtype: Int `{ return recv->h_addrtype; `}
        fun h_length: Int `{ return recv->h_length; `}
-       fun h_name: String `{ return new_String_from_cstring(recv->h_name); `}
+       fun h_name: String `{ return NativeString_to_s(recv->h_name); `}
 end
 
 extern FFTimeval `{ S_TIMEVAL* `}
index b210690..67b85f7 100644 (file)
@@ -60,14 +60,14 @@ extern class Sqlite3Code `{int`}
        new done `{ return SQLITE_DONE; `} #       101  /* sqlite3_step() has finished executing */
        fun is_done: Bool `{ return recv == SQLITE_DONE; `}
 
-       redef fun to_s: String import String::from_cstring `{
+       redef fun to_s: String import NativeString::to_s `{
 #if SQLITE_VERSION_NUMBER >= 3007015
                char *err = (char *)sqlite3_errstr(recv);
 #else
                char *err = "sqlite3_errstr supported only by version >= 3.7.15";
 #endif
                if (err == NULL) err = "";
-               return new_String_from_cstring(err);
+               return NativeString_to_s(err);
        `}
 end
 
@@ -77,13 +77,13 @@ extern class Statement `{sqlite3_stmt*`}
                return sqlite3_step(recv);
        `}
 
-       fun column_name(i: Int) : String import String::from_cstring `{
+       fun column_name(i: Int) : String import NativeString::to_s `{
                const char * name = (sqlite3_column_name(recv, i));
                if(name == NULL){
                        name = "";
                }
                char * ret = (char *) name;
-               return new_String_from_cstring(ret);
+               return NativeString_to_s(ret);
        `}
 
        fun column_bytes(i: Int) : Int `{
@@ -98,12 +98,12 @@ extern class Statement `{sqlite3_stmt*`}
                return sqlite3_column_int(recv, i);
        `}
 
-       fun column_text(i: Int) : String import String::from_cstring `{
+       fun column_text(i: Int) : String import NativeString::to_s `{
                char * ret = (char *) sqlite3_column_text(recv, i);
                if( ret == NULL ){
                        ret = "";
                }
-               return new_String_from_cstring(ret);
+               return NativeString_to_s(ret);
        `}
 
        fun column_type(i: Int) : Int `{
index 939c9a8..fb83040 100644 (file)
@@ -27,7 +27,7 @@ redef class String
                # FIXME: There is no proper way to handle NULL C string yet. What a pitty.
                var nulstr = once ("".to_cstring.get_environ)
                if res != nulstr then
-                       return new String.from_cstring(res)
+                       return res.to_s
                else
                        return ""
                end
index 79e717c..764ca75 100644 (file)
@@ -316,7 +316,7 @@ redef class String
        end
 
        # returns files contained within the directory represented by self
-       fun files : Set[ String ] is extern import HashSet, HashSet::add, String::from_cstring, String::to_cstring, HashSet[String] as( Set[String] ), String as( Object )
+       fun files : Set[ String ] is extern import HashSet, HashSet::add, NativeString::to_s, String::to_cstring, HashSet[String] as( Set[String] ), String as( Object )
 end
 
 redef class NativeString
index 56b26da..ca02ba1 100644 (file)
@@ -31,7 +31,7 @@ C implementation of file::String::files
 Imported methods signatures:
        HashSet new_HashSet(  ) for hash_collection::HashSet::init
        void HashSet_add( HashSet recv, Object item ) for hash_collection::HashSet::(abstract_collection::SimpleCollection::add)
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s() for string::NativeString::to_s
        int HashSet_is_a_Set( HashSet value ) to check if a HashSet[String] is a Set[String]
        Set HashSet_as_Set( HashSet value ) to cast from HashSet[String] to Set[String]
 */
@@ -58,7 +58,7 @@ Set String_files___impl( String recv )
                        if ( strcmp( de->d_name, ".." ) != 0 &&
                                strcmp( de->d_name, "." ) != 0 )
                        {
-                               file_name = new_String_from_cstring( strdup( de->d_name ) );
+                               file_name = NativeString_to_s( strdup( de->d_name ) );
                                HashSet_add( results, String_as_Object( file_name ) );
                        }
 
index bf569c2..f62f988 100644 (file)
@@ -334,7 +334,7 @@ class String
 
                outstr[self.length] = '\0'
 
-               return new String.with_native(outstr, self._length)
+               return outstr.to_s_with_length(self._length)
        end
 
        redef fun to_lower : String
@@ -354,7 +354,7 @@ class String
 
                outstr[self.length] = '\0'
 
-               return new String.with_native(outstr, self._length)
+               return outstr.to_s_with_length(self._length)
        end
 
        redef fun trim: String
@@ -406,7 +406,7 @@ class String
        end
 
        # Create a new string from a given char *.
-       init with_native(nat: NativeString, size: Int)
+       private init with_native(nat: NativeString, size: Int)
        do
                assert size >= 0
                _items = nat
@@ -416,7 +416,7 @@ class String
        end
 
        # Create a new string from a null terminated char *.
-       init from_cstring(str: NativeString)
+       private init from_cstring(str: NativeString)
        do
                with_native(str,str.cstring_length)
        end
@@ -425,7 +425,7 @@ class String
        # Pretty much equals to from_cstring but copies instead
        # of passing a reference
        # Avoids manual/automatic dealloc problems when dealing with native C code
-       init copy_from_native(str: NativeString)
+       private init copy_from_native(str: NativeString)
        do
                var temp_length = str.cstring_length
                var new_str = calloc_string(temp_length + 1)
@@ -434,10 +434,17 @@ class String
                with_native(new_str, temp_length)
        end
 
+       private init with_infos(items: NativeString, len: Int, from: Int, to: Int)
+       do
+               self._items = items
+               _length = len
+               _index_from = from
+               _index_to = to
+       end
+
        # Return a null terminated char *
        fun to_cstring: NativeString
        do
-               #return items
                if _index_from > 0 or _index_to != items.cstring_length - 1 then
                        var newItems = calloc_string(_length + 1)
                        self.items.copy_to(newItems, _length, _index_from, 0)
@@ -519,14 +526,16 @@ class String
                var my_length = self._length
                var its_length = s._length
 
+               var total_length = my_length + its_length
+
                var target_string = calloc_string(my_length + its_length + 1)
 
                self._items.copy_to(target_string, my_length, _index_from, 0)
                s._items.copy_to(target_string, its_length, s._index_from, my_length)
 
-               target_string[my_length + its_length] = '\0'
+               target_string[total_length] = '\0'
 
-               return new String.with_native(target_string, my_length + its_length)
+               return target_string.to_s_with_length(total_length)
        end
 
        # `i` repetitions of `self`
@@ -555,7 +564,7 @@ class String
                        current_last += my_length
                end
 
-               return new String.with_native(target_string, final_length)
+               return target_string.to_s_with_length(final_length)
        end
 
        redef fun to_s do return self
@@ -638,7 +647,7 @@ class Buffer
                # Ensure the afterlast byte is '\0' to nul-terminated char *
                a[length] = '\0'
 
-               return new String.with_native(a, length)
+               return a.to_s_with_length(length)
        end
 
        redef fun <(s)
@@ -719,7 +728,7 @@ redef class Object
        # The class name of the object.
        #
        #    assert 5.class_name == "Int"
-       fun class_name: String do return new String.from_cstring(native_class_name)
+       fun class_name: String do return native_class_name.to_s
 
        # Developer readable representation of `self`.
        # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
@@ -788,7 +797,7 @@ redef class Int
        #     assert (-123).to_s       == "-123"
        redef fun to_s do
                var len = digit_count(10)
-               return new String.from_cstring(native_int_to_s(len))
+               return native_int_to_s(len).to_s_with_length(len)
        end
 
        # return displayable int in hexadecimal (unsigned (not now))
@@ -847,7 +856,7 @@ redef class Float
                end
        end
 
-       fun to_precision_native(nb: Int): String import String::from_cstring `{
+       fun to_precision_native(nb: Int): String import NativeString::to_s `{
                int size;
                char *str;
 
@@ -855,7 +864,7 @@ redef class Float
                str = malloc(size + 1);
                sprintf(str, "%.*f", (int)nb, recv );
 
-               return new_String_from_cstring( str );
+               return NativeString_to_s( str );
        `}
 end
 
@@ -985,10 +994,12 @@ end
 
 # Native strings are simple C char *
 class NativeString
+       super StringCapable
+
        fun [](index: Int): Char is intern
        fun []=(index: Int, item: Char) is intern
        fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
-       
+
        # Position of the first nul character.
        fun cstring_length: Int
        do
@@ -998,6 +1009,26 @@ class NativeString
        end
        fun atoi: Int is intern
        fun atof: Float is extern "atof"
+
+       redef fun to_s
+       do
+               return to_s_with_length(cstring_length)
+       end
+
+       fun to_s_with_length(length: Int): String
+       do
+               assert length >= 0
+               return new String.with_infos(self, length, 0, length - 1)
+       end
+
+       fun to_s_with_copy: String
+       do
+               var length = cstring_length
+               var new_self = calloc_string(length + 1)
+               copy_to(new_self, length, 0, 0)
+               return new String.with_infos(new_self, length, 0, length - 1)
+       end
+
 end
 
 # StringCapable objects can create native strings
@@ -1017,7 +1048,7 @@ redef class Sys
        # The name of the program as given by the OS
        fun program_name: String
        do
-               return new String.from_cstring(native_argv(0))
+               return native_argv(0).to_s
        end
 
        # Initialize `args` with the contents of `native_argc` and `native_argv`.
@@ -1027,7 +1058,7 @@ redef class Sys
                var args = new Array[String].with_capacity(0)
                var i = 1
                while i < argc do
-                       args[i-1] = new String.from_cstring(native_argv(i))
+                       args[i-1] = native_argv(i).to_s
                        i += 1
                end
                _args_cache = args
index f391e51..df31613 100644 (file)
@@ -36,8 +36,8 @@ extern class TimeT `{time_t`}
 
        fun update `{ time(&recv); `}
 
-       fun ctime: String import String::copy_from_native `{
-               return new_String_copy_from_native( ctime(&recv) );
+       fun ctime: String import NativeString::to_s_with_copy `{
+               return NativeString_to_s_with_copy( ctime(&recv) );
        `}
 
        # Difference in secondes from start (self if the end time)
@@ -85,10 +85,10 @@ extern class Tm `{struct tm *`}
        fun yday: Int `{ return recv->tm_yday; `}
        fun is_dst: Bool `{ return recv->tm_isdst; `}
 
-       fun asctime: String import String::copy_from_native `{
-               return new_String_copy_from_native( asctime(recv) );
+       fun asctime: String import NativeString::to_s_with_copy `{
+               return NativeString_to_s_with_copy( asctime(recv) );
        `}
-       fun strftime(format: String): String import String::to_cstring, String::from_cstring `{
+       fun strftime(format: String): String import String::to_cstring, NativeString::to_s `{
                char* buf, *c_format;
                size_t res;
 
@@ -96,7 +96,7 @@ extern class Tm `{struct tm *`}
                c_format = String_to_cstring(format);
 
                res = strftime(buf, 100, c_format, recv);
-               return new_String_from_cstring(buf);
+               return NativeString_to_s(buf);
        `}
 
        redef fun to_s do return asctime.replace("\n", "")
index 008a510..34dd8c9 100644 (file)
@@ -1 +1 @@
-alt/error_needed_method_alt3.nit:48,9--13: Fatal Error: String must have a property named from_cstring.
+alt/error_needed_method_alt3.nit:48,9--13: Fatal Error: NativeString must have a property named to_s.
index b0340c8..34dd8c9 100644 (file)
@@ -1 +1 @@
-alt/error_needed_method_alt3.nit:48,1--56,3: Fatal Error: String must have a property named with_native.
+alt/error_needed_method_alt3.nit:48,9--13: Fatal Error: NativeString must have a property named to_s.
index d2c6277..70977d8 100644 (file)
@@ -28,13 +28,13 @@ class StringTest
                referenced_str = null
        end
 
-       fun get_c_string import String::items, String::from_cstring, String::copy_from_native, StringTest::ref_test, StringTest::copy_test `{
+       fun get_c_string import String::items, NativeString::to_s, NativeString::to_s_with_copy, StringTest::ref_test, StringTest::copy_test `{
                char* string = "This is a test string";
 
-               String ref_string = new_String_from_cstring(string);
+               String ref_string = NativeString_to_s(string);
                StringTest_ref_test(recv, ref_string);
 
-               String copy_string = new_String_copy_from_native(string);
+               String copy_string = NativeString_to_s_with_copy(string);
                StringTest_copy_test(recv, copy_string);
 
                int same_refs = String_items(copy_string) == String_items(ref_string);
index d33ae3c..dbe69c5 100644 (file)
@@ -24,16 +24,16 @@ class A
                        String_to_cstring( A_r( recv ) ),
                        String_to_cstring( A_rw( recv ) ) );
        `}
-       fun modify import String::from_cstring, w=, rw= `{
-               A_w__assign( recv, new_String_from_cstring( "w set from native" ) );
-               A_rw__assign( recv, new_String_from_cstring( "rw set from native" ) );
+       fun modify import NativeString::to_s, w=, rw= `{
+               A_w__assign( recv, NativeString_to_s( "w set from native" ) );
+               A_rw__assign( recv, NativeString_to_s( "rw set from native" ) );
        `}
 end
 
 class B
-       fun print_and_modify( a : A ) import A::rw, A::rw=, String::to_cstring, String::from_cstring `{
+       fun print_and_modify( a : A ) import A::rw, A::rw=, String::to_cstring, NativeString::to_s `{
                printf( "%s\n", String_to_cstring( A_rw( a ) ) );
-               A_rw__assign( a, new_String_from_cstring( "set from native" ) );
+               A_rw__assign( a, NativeString_to_s( "set from native" ) );
                printf( "%s\n", String_to_cstring( A_rw( a ) ) );
        `}
 end
index 7299f4a..1e1e0c5 100644 (file)
@@ -29,11 +29,11 @@ extern Test
         return foobar;
     `}
 
-    fun test_me is extern import Test::foo, String::from_cstring `{
+    fun test_me is extern import Test::foo, NativeString::to_s `{
         int i;
         for(i = 0; i < 2000; ++i) {
             printf("%d\n", i);
-            Test_foo(recv, new_String_from_cstring("asdf"));
+            Test_foo(recv, NativeString_to_s("asdf"));
         }
     `}
 
index d268855..e795227 100644 (file)
@@ -38,8 +38,8 @@ extern A
 
        fun m : Int is extern `{ return 10; `}
 
-       fun n : String is extern import String::from_cstring `{
-               return new_String_from_cstring( "allo" );
+       fun n : String is extern import NativeString::to_s `{
+               return NativeString_to_s( "allo" );
        `}
 
        fun o ( str : String ) is extern import String::to_cstring `{
index 9af7023..aad8b05 100644 (file)
@@ -26,8 +26,8 @@ fun print_string(str: String) import String::to_cstring `{
        printf("str-> %s\n", String_to_cstring(str) );
 `}
 
-fun get_string: String import String::from_cstring, String::output `{
-       String str = new_String_from_cstring("Nit string from C");
+fun get_string: String import NativeString::to_s, String::output `{
+       String str = NativeString_to_s("Nit string from C");
        String_output(str);
        printf("\n");
        return str;
index d9672bc..68c20f8 100644 (file)
@@ -21,7 +21,7 @@ end
 class B
        super A
 
-       redef fun id : String import super, String::from_cstring, String::to_cstring `{
+       redef fun id : String import super, NativeString::to_s, String::to_cstring `{
                char *new_name;
                char *prefix = "B special ";
                char *super_name = String_to_cstring( B_id___super( recv ) );
@@ -31,7 +31,7 @@ class B
                strcpy( new_name+strlen( prefix ), super_name );
                new_name[ strlen( prefix )+strlen( super_name ) ] = '\0';
 
-               return new_String_from_cstring( new_name );
+               return NativeString_to_s( new_name );
        `}
 end
 
index 6f2a5d3..dd13759 100644 (file)
@@ -20,11 +20,11 @@ class A
        readable writable var _rw : String = "rw"
 
        fun print_all is extern import String::to_cstring, r, rw
-       fun modify is extern import String::from_cstring, w=, rw=
+       fun modify is extern import NativeString::to_s, w=, rw=
 end
 
 class B
-       fun print_and_modify( a : A ) is extern import A::rw, A::rw=, String::to_cstring, String::from_cstring
+       fun print_and_modify( a : A ) is extern import A::rw, A::rw=, String::to_cstring, NativeString::to_s
 end
 
 var a = new A
index e41a6a7..c9e54a2 100644 (file)
@@ -32,14 +32,14 @@ void A_print_all___impl( A recv )
 C implementation of test_ni_accessor::A::modify
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
        void A_w__eq( A recv, String w ) for test_ni_accessor::A::w=
        void A_rw__eq( A recv, String rw ) for test_ni_accessor::A::rw=
 */
 void A_modify___impl( A recv )
 {
-       A_w__assign( recv, new_String_from_cstring( "w set from native" ) );
-       A_rw__assign( recv, new_String_from_cstring( "rw set from native" ) );
+       A_w__assign( recv, NativeString_to_s( "w set from native" ) );
+       A_rw__assign( recv, NativeString_to_s( "rw set from native" ) );
 }
 
 /*
@@ -49,11 +49,11 @@ Imported methods signatures:
        String A_rw( A recv ) for test_ni_accessor::A::rw
        void A_rw__assign( A recv, String value ) for test_ni_accessor::A::rw=
        char * String_to_cstring( String recv ) for string::String::to_cstring
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 void B_print_and_modify___impl( B recv, A a )
 {
        printf( "%s\n", String_to_cstring( A_rw( a ) ) );
-       A_rw__assign( a, new_String_from_cstring( "set from native" ) );
+       A_rw__assign( a, NativeString_to_s( "set from native" ) );
        printf( "%s\n", String_to_cstring( A_rw( a ) ) );
 }
index f3ea67c..7003fab 100644 (file)
@@ -44,7 +44,7 @@ void A_set___impl( A recv, char v )
 C implementation of test_ni_inits::A::(string::Object::to_s)
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String A_to_s___impl( A recv )
 {
@@ -52,5 +52,5 @@ String A_to_s___impl( A recv )
        str[0] = *recv;
        str[1] = 0;
 
-       return new_String_from_cstring( str );
+       return NativeString_to_s( str );
 }
index ea2b78a..22941b4 100644 (file)
@@ -18,7 +18,7 @@ class A
        fun is_int_null( ni : nullable Int ) : Bool is extern import Int as not nullable
        fun is_a_null( na : nullable A ) : Bool is extern import A as not nullable
 
-       fun get_nullable_string( get_nulled : Bool ) : nullable String is extern import String::from_cstring, String as nullable
+       fun get_nullable_string( get_nulled : Bool ) : nullable String is extern import NativeString::to_s, String as nullable
 end
 
 var a = new A
index 9a42abe..1265225 100644 (file)
@@ -41,7 +41,7 @@ int A_is_a_null___impl( A recv, nullable_A na )
 C implementation of test_ni_null::A::get_nullable_string
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
        nullable_String String_as_nullable( String value ) to cast from String to nullable String
 */
 nullable_String A_get_nullable_string___impl( A recv, int get_nulled )
@@ -52,6 +52,6 @@ nullable_String A_get_nullable_string___impl( A recv, int get_nulled )
     }
     else
     {
-        return String_as_nullable( new_String_from_cstring( "something" ) );
+        return String_as_nullable( NativeString_to_s( "something" ) );
     }
 }
index 896d641..85a06c7 100644 (file)
@@ -18,7 +18,7 @@ extern A
 super Pointer
        new is extern
 
-       redef fun to_s : String is extern import String::from_cstring
+       redef fun to_s : String is extern import NativeString::to_s
 end
 
 var a = new A
index ae916ce..00680a2 100644 (file)
@@ -27,11 +27,11 @@ A new_A___impl(  )
 C implementation of test_ni_pointer::A::(string::Object::to_s)
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String A_to_s___impl( A recv )
 {
        char* s = calloc( 256, sizeof(char) );
        sprintf( s, "%d", *((int*)recv) );
-       return new_String_from_cstring( s );
+       return NativeString_to_s( s );
 }
index 6c4a43c..afd9833 100644 (file)
@@ -19,7 +19,7 @@ extern ExternInt super Pointer
        new as_1 is extern
        new as_2 is extern
 
-       redef fun to_s : String is extern import String::from_cstring
+       redef fun to_s : String is extern import NativeString::to_s
        fun to_i : Int is extern
 end
 
index 00ccaf2..f26fc6d 100644 (file)
@@ -47,7 +47,7 @@ ExternInt new_ExternInt_as_2___impl(  )
 C implementation of test_ni_special_pointer::ExternInt::(string::Object::to_s)
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String ExternInt_to_s___impl( ExternInt recv )
 {
@@ -55,7 +55,7 @@ String ExternInt_to_s___impl( ExternInt recv )
        char *vs = (char*)calloc( 256, sizeof(char) );
 
        sprintf( vs, "%i", v );
-       return new_String_from_cstring( vs );
+       return NativeString_to_s( vs );
 }
 
 /*
index ad05cb3..60af299 100644 (file)
 # limitations under the License.
 
 class A
-       fun get_str_from_nstr( nstr : NativeString ) : String is extern import String::from_cstring
-       fun get_str_from_nstr_with_len( nstr : NativeString ) : String is extern import String::with_native, NativeString::cstring_length
+       fun get_str_from_nstr( nstr : NativeString ) : String is extern import NativeString::to_s
+       fun get_str_from_nstr_with_len( nstr : NativeString ) : String is extern import NativeString::to_s_with_length, NativeString::cstring_length
 
        fun get_nstr_from_str( str : String ) : NativeString is extern import String::to_cstring
 
-       fun get_something : String is extern import String::from_cstring
+       fun get_something : String is extern import NativeString::to_s
 end
 
 var a = new A
 print a.get_str_from_nstr( "hello world...".to_cstring )
 print a.get_str_from_nstr_with_len( "hello world?".to_cstring )
-print new String.from_cstring( a.get_nstr_from_str( "hello world!" ) )
+print a.get_nstr_from_str( "hello world!" ).to_s
 
 
 var something = a.get_something # expects "something"
index 57dd878..2a23e1c 100644 (file)
 C implementation of test_ni_strings::A::get_str_from_nstr
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String A_get_str_from_nstr___impl( A recv, char * nstr )
 {
-       return new_String_from_cstring( nstr );
+       return NativeString_to_s( nstr );
 }
 
 /*
 C implementation of test_ni_strings::A::get_str_from_nstr_with_len
 
 Imported methods signatures:
-       String new_String_with_native( char * nat, bigint size ) for string::String::with_native
+       String NativeString_to_s_with_length( char * nat, bigint size ) for string::NativeString::to_s_with_length
        bigint NativeString_cstring_length( char * recv ) for string::NativeString::cstring_length
 */
 String A_get_str_from_nstr_with_len___impl( A recv, char * nstr )
 {
-       return new_String_with_native( nstr, NativeString_cstring_length( nstr ) );
+       return NativeString_to_s_with_length( nstr, NativeString_cstring_length( nstr ) );
 }
 
 /*
@@ -53,9 +53,9 @@ char * A_get_nstr_from_str___impl( A recv, String str )
 C implementation of test_ni_strings::A::get_something
 
 Imported methods signatures:
-       String new_String_from_cstring( char * str ) for string::String::from_cstring
+       String NativeString_to_s( char * str ) for string::NativeString::to_s
 */
 String A_get_something___impl( A recv )
 {
-       return new_String_from_cstring( "something" );
+       return NativeString_to_s( "something" );
 }
index 2595e8a..e0c0d1b 100644 (file)
@@ -20,7 +20,7 @@ end
 
 class B
 super A
-       redef fun id : String is extern import super, String::from_cstring, String::to_cstring
+       redef fun id : String is extern import super, NativeString::to_s, String::to_cstring
 end
 
 var a = new A
index fc4ca50..90a0f37 100644 (file)
@@ -17,7 +17,7 @@
 C implementation of super::B::(super::A::id)
 
 Imported methods signatures:
-       void new_String_from_cstring( char * str ) for string::String::from_cstring
+       void NativeString_to_s( char * str ) for string::NativeString::to_s
        char * String_to_cstring( String recv ) for string::String::to_cstring
        String B_id___super( B recv ) to call super
 */
@@ -32,5 +32,5 @@ String B_id___impl( B recv )
        strcpy( new_name+strlen( prefix ), super_name );
        new_name[ strlen( prefix )+strlen( super_name ) ] = '\0';
 
-       return new_String_from_cstring( new_name );
+       return NativeString_to_s( new_name );
 }