midpstring.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 725 行 · 第 1/2 页

C
725
字号
/* *    * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */#include <string.h>#include <midpMalloc.h>#include <midpString.h>#include <pcsl_memory.h>/** * @file * * This is the native Unicode string API. It is modeled after the * java.lang.String class. *//** Global NULL string. */const MidpString NULL_MIDP_STRING = { NULL_LEN, NULL };/** Global empty string. */const MidpString EMPTY_MIDP_STRING = { 0, NULL };/** * Concatenates 2 unicode strings into 1 string. Free the results data * field with midpFree. *  * @param str1 first string * @param str2 second string * * @return result of first + second, len is OUT_OF_MEM_STR_LEN if out of memory */MidpString midpStringCatImpl(MidpString str1, MidpString str2,        char* filename, int line) {    MidpString result;    (void)filename;                               /* Avoid compiler warnings */    (void)line;                                   /* Avoid compiler warnings */    if (str1.len < 0) {        str1.len = 0;    }    if (str2.len < 0) {        str2.len = 0;    }    result.len = str1.len + str2.len;    if (result.len == 0) {        result.data = NULL;        return result;    }    result.data = (jchar*)midpMallocImpl(result.len * sizeof (jchar),                                         filename, line);    if (result.data == NULL) {        result.len = OUT_OF_MEM_LEN;        return result;    }    memcpy(result.data, str1.data, str1.len * sizeof (jchar));    memcpy(result.data + str1.len, str2.data, str2.len * sizeof (jchar));    return result;}/** * Compares 2 unicode strings. *  * @param str1 first string * @param str2 second string * * @return 0 if str1 = str2, < 0 if str1 < str2, > 0 if str1 > str2 */int midpStringCmp(MidpString str1, MidpString str2) {    int i;    for (i = 0; i < str1.len && i < str2.len; i++) {        if (str1.data[i] < str2.data[i]) {            return -1;        }        if (str1.data[i] > str2.data[i]) {            return 1;        }    }    if (str1.len < str2.len) {        return -1;    }    if (str1.len > str2.len) {        return 1;    }    return 0;}/** * Check to see if 2 unicode strings are equal. *  * @param str1 first string * @param str2 second string * * @return true if equal false if not */int midpStringEquals(MidpString str1, MidpString str2) {    return midpStringCmp(str1, str2) == 0;}/** * Check to see if one string ends with another. *  * @param str1 first string * @param str2 second string * * @return 1 if str1 ends with str2, else 0 */int midpStringEndsWith(MidpString str1, MidpString str2) {    int i;    int j;    if (str1.len < str2.len) {        return 0;    }    if (str2.len == 0) {        return 1;    }    for (i = str1.len - 1, j = str2.len - 1; j >= 0; i--, j--) {        if (str1.data[i] != str2.data[j]) {            return 0;        }    }    return 1;}/** * Convert a jchar string to a C string. * This function should not be used directly, * use the midpJcharsToChars macro. * * @param in jchar string * @param filename provided by the midpJcharsToChars macro * @param line provided by the midpJcharsToChars macro * * @return C string or NULL */char* midpJcharsToCharsImpl(MidpString in, char* filename, int line) {    char* out;    int i;    (void)filename;                               /* Avoid compiler warnings */    (void)line;                                   /* Avoid compiler warnings */    if (in.len < 0) {        return NULL;    }    out = (char*)midpMallocImpl(in.len + 1, filename, line);    if (out == NULL) {        return NULL;    }    for (i = 0; i < in.len; i++) {        out[i] = (char)in.data[i];    }    out[i] = 0;    return out;}/** * Convert a C string to a jchar string. * This function should not be used directly, * use the midpCharsToJchars macro. * * @param in C string * @param filename provided by the midpCharsToJchars macro * @param line provided by the midpCharsToJchars macro * * @return jchar string */MidpString midpCharsToJcharsImpl(char* in, char* filename, int line) {    MidpString out;    int i;    (void)filename;                               /* Avoid compiler warnings */    (void)line;                                   /* Avoid compiler warnings */    if (in == NULL) {        out.len = NULL_LEN;        out.data = NULL;        return out;    }    out.len = strlen(in);    if (out.len == 0) {        out.data = NULL;        return out;    }    out.data = (jchar*)midpMallocImpl(out.len * sizeof (jchar), filename,                                      line);    if (out.data == NULL) {        out.len = OUT_OF_MEM_LEN;        return out;    }    for (i = 0; i < out.len; i++) {        out.data[i] = (jchar)in[i];    }    return out;}/** * Duplicates a unicode strings. Free the results data * field with midpFree. * This function should not be used directly, * use the midpStringDup macro. *  * @param in input string * @param filename provided by the midpStringDup macro * @param line provided by the midpStringDup macro * * @return copy of the input string */MidpString midpStringDupImpl(MidpString in, char* filename, int line) {    return midpSubStringImpl(in, 0, in.len, filename, line);}/** * Frees a MIDP string. * * @param str string to free */void midpFreeString(MidpString str) {    if (str.len <= 0 || str.data == NULL) {        return;    }    midpFree(str.data);}/** * Returns a new string that is a substring of this string. The * substring begins at the specified <code>beginIndex</code> and * extends to the character at index <code>endIndex - 1</code>. * Thus the length of the substring is <code>endIndex-beginIndex</code>. * <p> * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * </pre></blockquote> * * This function should not be used directly, * use the midpStringDup macro. * * @param in input string * @param beginIndex the beginning index, inclusive. * @param endIndex the ending index, exclusive. * @param filename provided by the midpSubString macro * @param line provided by the midpSubString macro * * @return     the specified substring, or a string with a length of *             OUT_OF_MEM_LEN. */MidpString midpSubStringImpl(MidpString in, int beginIndex, int endIndex,        char* filename, int line) {    MidpString result;    (void)filename;                               /* Avoid compiler warnings */    (void)line;                                   /* Avoid compiler warnings */    result.data = NULL;    if (in.len < 0 || endIndex > in.len || beginIndex < 0 ||            beginIndex >= endIndex) {        result.len = NULL_LEN;        return result;    }    if (in.len == 0) {        result.len = 0;        return result;    }    result.len = endIndex - beginIndex;    result.data = (jchar*)midpMallocImpl(result.len * sizeof (jchar),                  filename, line);    if (result.data == NULL) {        result.len = OUT_OF_MEM_LEN;        return result;    }    memcpy(result.data, in.data + beginIndex, result.len * sizeof (jchar));    return result;}/** * Returns the index within this string of the last occurrence of the * specified character. That is, the index returned is the largest * value <i>k</i> such that: * <blockquote><pre> * string.data[<i>k</i>] == ch * </pre></blockquote> * is true. * The String is searched backwards starting at the last character. * * @param   ch   a character. * @return  the index of the last occurrence of the character in the *          character sequence represented by this object, or *          <code>-1</code> if the character does not occur. */int midpStringLastIndexOf(MidpString str, jchar ch) {    int i;    if (str.len <= 0) {        return  -1;    }    for (i = str.len - 1; i >= 0; i--) {        if (str.data[i] == ch) {            return i;        }    }    return -1;} /** * looks for first occurrence of <PARAM>c</PARAM> in the <PARAM>line</PARAM>  *  * @param c    character to look for * @param line   MidpString to search in * @return index of c */int midpStringIndexOf(MidpString line, jchar c) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?