X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/string.nit b/lib/standard/string.nit index 1c7e953..bcd0445 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -15,13 +15,14 @@ package string intrude import collection # FIXME should be collection::array +import hash ############################################################################### # String # ############################################################################### abstract class AbstractString -special AbstractArrayRead[Char] + super AbstractArrayRead[Char] readable private var _items: NativeString redef fun [](index) do return _items[index] @@ -159,8 +160,8 @@ end class String -special Comparable -special AbstractString + super Comparable + super AbstractString redef type OTHER: String # Create a new string from a given char *. @@ -246,14 +247,28 @@ special AbstractString end redef fun to_s do return self + + redef fun hash + do + # djb2 hash algorythm + var h = 5381 + var i = _length - 1 + var it = _items + while i >= 0 do + h = (h * 32) + h + it[i].ascii + i -= 1 + end + return h + + end end # Strings are arrays of characters. class Buffer -special AbstractString -special Comparable -special StringCapable -special AbstractArray[Char] + super AbstractString + super Comparable + super StringCapable + super AbstractArray[Char] redef type OTHER: String @@ -383,23 +398,28 @@ redef class Object # User redeable representation of `self'. fun to_s: String do return inspect + # The class name of the object in NativeString format. + private fun native_class_name: NativeString is intern + + # The class name of the object. + # FIXME: real type information is not available at runtime. Therefore, for instance, an instance of List[Bool] has just "List" for classname + fun class_name: String do return new String.from_cstring(native_class_name) + # Developper readable representation of `self'. # Usualy, it uses the form "" fun inspect: String do - var r = inspect_head - # r.add('>') - return r + return "<{inspect_head}>" end - # Return "