Make some of the StringObject functions member functions.

Change-Id: I72ed13c16f0cb24498772c453ba268a0f65f208a
diff --git a/vm/CheckJni.cpp b/vm/CheckJni.cpp
index 7339d9b..d1ecd10 100644
--- a/vm/CheckJni.cpp
+++ b/vm/CheckJni.cpp
@@ -1569,7 +1569,7 @@
     if (gDvmJni.forceCopy && result != NULL) {
         ScopedJniThreadState ts(env);
         StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
-        int byteCount = dvmStringLen(strObj) * 2;
+        int byteCount = strObj->length() * 2;
         result = (const jchar*) GuardedCopy::create(result, byteCount, false);
         if (isCopy != NULL) {
             *isCopy = JNI_TRUE;
@@ -1832,7 +1832,7 @@
     if (gDvmJni.forceCopy && result != NULL) {
         ScopedJniThreadState ts(env);
         StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
-        int byteCount = dvmStringLen(strObj) * 2;
+        int byteCount = strObj->length() * 2;
         result = (const jchar*) GuardedCopy::create(result, byteCount, false);
         if (isCopy != NULL) {
             *isCopy = JNI_TRUE;
diff --git a/vm/Ddm.cpp b/vm/Ddm.cpp
index ff853e5..e370204 100644
--- a/vm/Ddm.cpp
+++ b/vm/Ddm.cpp
@@ -246,8 +246,9 @@
  */
 void dvmDdmSendThreadNotification(Thread* thread, bool started)
 {
-    if (!gDvm.ddmThreadNotification)
+    if (!gDvm.ddmThreadNotification) {
         return;
+    }
 
     StringObject* nameObj = NULL;
     Object* threadObj = thread->threadObj;
@@ -268,16 +269,17 @@
         type = CHUNK_TYPE("THCR");
 
         if (nameObj != NULL) {
-            stringLen = dvmStringLen(nameObj);
-            chars = dvmStringChars(nameObj);
+            stringLen = nameObj->length();
+            chars = nameObj->chars();
         } else {
             stringLen = 0;
             chars = NULL;
         }
 
         /* leave room for the two integer fields */
-        if (stringLen > (sizeof(buf) - sizeof(u4)*2) / 2)
+        if (stringLen > (sizeof(buf) - sizeof(u4)*2) / 2) {
             stringLen = (sizeof(buf) - sizeof(u4)*2) / 2;
+        }
         len = stringLen*2 + sizeof(u4)*2;
 
         set4BE(&buf[0x00], thread->threadId);
@@ -285,8 +287,9 @@
 
         /* copy the UTF-16 string, transforming to big-endian */
         outChars = (u2*)(void*)&buf[0x08];
-        while (stringLen--)
+        while (stringLen--) {
             set2BE((u1*) (outChars++), *chars++);
+        }
     } else {
         type = CHUNK_TYPE("THDE");
 
@@ -303,11 +306,12 @@
  */
 void dvmDdmSendThreadNameChange(int threadId, StringObject* newName)
 {
-    if (!gDvm.ddmThreadNotification)
+    if (!gDvm.ddmThreadNotification) {
         return;
+    }
 
-    size_t stringLen = dvmStringLen(newName);
-    const u2* chars = dvmStringChars(newName);
+    size_t stringLen = newName->length();
+    const u2* chars = newName->chars();
 
     /*
      * Output format:
@@ -321,8 +325,9 @@
     set4BE(&buf[0x00], threadId);
     set4BE(&buf[0x04], stringLen);
     u2* outChars = (u2*)(void*)&buf[0x08];
-    while (stringLen--)
+    while (stringLen--) {
         set2BE((u1*) (outChars++), *chars++);
+    }
 
     dvmDbgDdmSendChunk(CHUNK_TYPE("THNM"), bufLen, buf);
 }
diff --git a/vm/Jni.cpp b/vm/Jni.cpp
index 70b1842..49a2c8a 100644
--- a/vm/Jni.cpp
+++ b/vm/Jni.cpp
@@ -389,11 +389,14 @@
         dvmDumpThread(dvmThreadSelf(), false);
         dvmAbort();     // spec says call FatalError; this is equivalent
     } else {
-        LOGVV("LREF add %p  (%s.%s) (ent=%d)", obj,
-            dvmGetCurrentJNIMethod()->clazz->descriptor,
-            dvmGetCurrentJNIMethod()->name,
-            (int) dvmReferenceTableEntries(pRefTable));
+        if (false) {
+            LOGI("LREF add %p  (%s.%s) (ent=%d)", obj,
+                    dvmGetCurrentJNIMethod()->clazz->descriptor,
+                    dvmGetCurrentJNIMethod()->name,
+                    (int) dvmIndirectRefTableEntries(pRefTable));
+        }
     }
+
     return jobj;
 }
 
@@ -2147,7 +2150,7 @@
 static jsize GetStringLength(JNIEnv* env, jstring jstr) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    return dvmStringLen(strObj);
+    return strObj->length();
 }
 
 
@@ -2161,11 +2164,11 @@
     ScopedJniThreadState ts(env);
 
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    ArrayObject* strChars = dvmStringCharArray(strObj);
+    ArrayObject* strChars = strObj->array();
 
     pinPrimitiveArray(strChars);
 
-    const u2* data = dvmStringChars(strObj);
+    const u2* data = strObj->chars();
     if (isCopy != NULL) {
         *isCopy = JNI_FALSE;
     }
@@ -2178,7 +2181,7 @@
 static void ReleaseStringChars(JNIEnv* env, jstring jstr, const jchar* chars) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    ArrayObject* strChars = dvmStringCharArray(strObj);
+    ArrayObject* strChars = strObj->array();
     unpinPrimitiveArray(strChars);
 }
 
@@ -2206,7 +2209,10 @@
 static jsize GetStringUTFLength(JNIEnv* env, jstring jstr) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    return dvmStringUtf8ByteLen(strObj);
+    if (strObj == NULL) {
+        return 0; // Should we throw something or assert?
+    }
+    return strObj->utfLength();
 }
 
 /*
@@ -2583,12 +2589,12 @@
 static void GetStringRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, jchar* buf) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    int strLen = dvmStringLen(strObj);
-    if (((start|len) < 0) || (start + len > dvmStringLen(strObj))) {
+    int strLen = strObj->length();
+    if (((start|len) < 0) || (start + len > strLen)) {
         dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
         return;
     }
-    memcpy(buf, dvmStringChars(strObj) + start, len * sizeof(u2));
+    memcpy(buf, strObj->chars() + start, len * sizeof(u2));
 }
 
 /*
@@ -2598,12 +2604,12 @@
 static void GetStringUTFRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, char* buf) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    int strLen = dvmStringLen(strObj);
-    if (((start|len) < 0) || (start + len > dvmStringLen(strObj))) {
+    int strLen = strObj->length();
+    if (((start|len) < 0) || (start + len > strLen)) {
         dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
         return;
     }
-    dvmCreateCstrFromStringRegion(strObj, start, len, buf);
+    dvmGetStringUtfRegion(strObj, start, len, buf);
 }
 
 /*
@@ -2643,11 +2649,11 @@
     ScopedJniThreadState ts(env);
 
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    ArrayObject* strChars = dvmStringCharArray(strObj);
+    ArrayObject* strChars = strObj->array();
 
     pinPrimitiveArray(strChars);
 
-    const u2* data = dvmStringChars(strObj);
+    const u2* data = strObj->chars();
     if (isCopy != NULL) {
         *isCopy = JNI_FALSE;
     }
@@ -2660,7 +2666,7 @@
 static void ReleaseStringCritical(JNIEnv* env, jstring jstr, const jchar* carray) {
     ScopedJniThreadState ts(env);
     StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
-    ArrayObject* strChars = dvmStringCharArray(strObj);
+    ArrayObject* strChars = strObj->array();
     unpinPrimitiveArray(strChars);
 }
 
diff --git a/vm/ReferenceTable.cpp b/vm/ReferenceTable.cpp
index a328ec7..5a16f35 100644
--- a/vm/ReferenceTable.cpp
+++ b/vm/ReferenceTable.cpp
@@ -284,6 +284,7 @@
                 extras += "\"";
             } else {
                 StringAppendF(&extras, "... (%d chars)", dvmStringLen(str));
+                StringAppendF(&extras, "... (%d chars)", str->length());
             }
             free(s);
         }
diff --git a/vm/UtfString.cpp b/vm/UtfString.cpp
index 05bac53..63d116e 100644
--- a/vm/UtfString.cpp
+++ b/vm/UtfString.cpp
@@ -305,63 +305,42 @@
     return newStr;
 }
 
-/*
- * Create a UTF-8 C string from a region of a java/lang/String.  (Used by
- * the JNI GetStringUTFRegion call.)
- */
-void dvmCreateCstrFromStringRegion(const StringObject* jstr,
+void dvmGetStringUtfRegion(const StringObject* jstr,
         int start, int len, char* buf)
 {
-    const u2* data = dvmStringChars(jstr) + start;
+    const u2* data = jstr->chars() + start;
     convertUtf16ToUtf8(buf, data, len);
 }
 
-/*
- * Compute the length, in modified UTF-8, of a java/lang/String object.
- *
- * Does not include the terminating null byte.
- */
-int dvmStringUtf8ByteLen(const StringObject* jstr)
+int StringObject::utfLength() const
 {
     assert(gDvm.classJavaLangString != NULL);
-    if (jstr == NULL) {
-        return 0;       // should we throw something?  assert?
-    }
 
-    int len = dvmGetFieldInt(jstr, STRING_FIELDOFF_COUNT);
-    int offset = dvmGetFieldInt(jstr, STRING_FIELDOFF_OFFSET);
+    int len = dvmGetFieldInt(this, STRING_FIELDOFF_COUNT);
+    int offset = dvmGetFieldInt(this, STRING_FIELDOFF_OFFSET);
     ArrayObject* chars =
-            (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+            (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
     const u2* data = (const u2*)(void*)chars->contents + offset;
     assert(offset + len <= (int) chars->length);
 
     return utf16_utf8ByteLen(data, len);
 }
 
-/*
- * Get the string's length.
- */
-int dvmStringLen(const StringObject* jstr)
+int StringObject::length() const
 {
-    return dvmGetFieldInt(jstr, STRING_FIELDOFF_COUNT);
+    return dvmGetFieldInt(this, STRING_FIELDOFF_COUNT);
 }
 
-/*
- * Get the char[] object from the String.
- */
-ArrayObject* dvmStringCharArray(const StringObject* jstr)
+ArrayObject* StringObject::array() const
 {
-    return (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+    return (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
 }
 
-/*
- * Get the string's data.
- */
-const u2* dvmStringChars(const StringObject* jstr)
+const u2* StringObject::chars() const
 {
-    int offset = dvmGetFieldInt(jstr, STRING_FIELDOFF_OFFSET);
+    int offset = dvmGetFieldInt(this, STRING_FIELDOFF_OFFSET);
     ArrayObject* chars =
-            (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+            (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
     return (const u2*)(void*)chars->contents + offset;
 }
 
diff --git a/vm/UtfString.h b/vm/UtfString.h
index b236ce5..352948c 100644
--- a/vm/UtfString.h
+++ b/vm/UtfString.h
@@ -129,31 +129,10 @@
  * Create a UTF-8 C string from a region of a java/lang/String.  (Used by
  * the JNI GetStringUTFRegion call.)
  */
-void dvmCreateCstrFromStringRegion(const StringObject* jstr,
+void dvmGetStringUtfRegion(const StringObject* jstr,
         int start, int len, char* buf);
 
 /*
- * Compute the length in bytes of the modified UTF-8 representation of a
- * string.
- */
-int dvmStringUtf8ByteLen(const StringObject* jstr);
-
-/*
- * Get the length in Unicode characters of a string.
- */
-int dvmStringLen(const StringObject* jstr);
-
-/*
- * Get the char[] object from the String.
- */
-ArrayObject* dvmStringCharArray(const StringObject* jstr);
-
-/*
- * Get a pointer to the Unicode data.
- */
-const u2* dvmStringChars(const StringObject* jstr);
-
-/*
  * Compare two string objects.  (This is a dvmHashTableLookup() callback.)
  */
 int dvmHashcmpStrings(const void* vstrObj1, const void* vstrObj2);
diff --git a/vm/oo/Object.h b/vm/oo/Object.h
index f43ecb5..b7786d2 100644
--- a/vm/oo/Object.h
+++ b/vm/oo/Object.h
@@ -245,6 +245,21 @@
 struct StringObject : Object {
     /* variable #of u4 slots; u8 uses 2 slots */
     u4              instanceData[1];
+
+    /** Returns this string's length in characters. */
+    int length() const;
+
+    /**
+     * Returns this string's length in bytes when encoded as modified UTF-8.
+     * Does not include a terminating NUL byte.
+     */
+    int utfLength() const;
+
+    /** Returns this string's char[] as an ArrayObject. */
+    ArrayObject* array() const;
+
+    /** Returns this string's char[] as a u2*. */
+    const u2* chars() const;
 };