⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hnstringobj.cpp

📁 SR-tree is an index structure for high-dimensional nearest neighbor queries
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * HnStringObj.cc
 * Copyright (C) 1997 Norio Katayama
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 *
 * 11/20/95 katayama@rd.nacsis.ac.jp
 * $Id: HnStringObj.cc,v 1.5 2000/06/10 11:11:57 katayama Exp $
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "HnSRTree/HnString.hh"
#include "HnSRTree/HnStringObj.hh"

HnStringObj::HnStringObj(void)
{
    initialize();

    buffer = (char *)HnMalloc(1);
    len = 0;
    buffer[len] = 0;
}

HnStringObj::HnStringObj(const char *ptr, int n)
{
    initialize();

    buffer = (char *)HnMalloc(n + 1);
    memcpy(buffer, ptr, n);
    len = n;
    buffer[len] = 0;
}

HnStringObj::HnStringObj(const HnString &string)
{
    const char *ptr = string.chars();
    int n = string.length();

    initialize();

    buffer = (char *)HnMalloc(n + 1);
    memcpy(buffer, ptr, n);
    len = n;
    buffer[len] = 0;
}

HnStringObj::HnStringObj(const char *ptr1, int n1, const char *ptr2, int n2)
{
    initialize();

    buffer = (char *)HnMalloc(n1 + n2 + 1);
    memcpy(buffer, ptr1, n1);
    memcpy(buffer + n1, ptr2, n2);
    len = n1 + n2;
    buffer[len] = 0;
}

HnStringObj::~HnStringObj(void)
{
    dispose();
}

HnString
HnStringObj::concat(const char *ptr, int n) const
{
    if ( n == 0 ) {
	return this;
    }
    else {
	return new HnStringObj(chars(), length(), ptr, n);
    }
}

HnString
HnStringObj::concat(const char *ptr) const
{
    return concat(ptr, ((ptr == NULL) ? 0 : strlen(ptr)));
}

HnString
HnStringObj::concat(const HnString &string) const
{
    return concat(string.getObject()->chars(), string.getObject()->length());
}

HnBool
HnStringObj::equals(const HnString &string) const
{
    if ( len == string.length() && strcmp(buffer, string.chars()) == 0 ) {
	return HnTRUE;
    }
    else {
	return HnFALSE;
    }
}

int
HnStringObj::compareTo(const HnString &string) const
{
    return strcmp(buffer, string.chars());
}

HnBool
HnStringObj::startsWith(const char *prefix, int n) const
{
    if ( n > len ) {
	return HnFALSE;
    }
    else {
	return (strncmp(chars(), prefix, n) == 0);
    }
}

HnBool
HnStringObj::startsWith(const char *prefix) const
{
    return startsWith(prefix, strlen(prefix));
}

HnBool
HnStringObj::startsWith(const HnString &string) const
{
    return startsWith(string.getObject()->chars(),
		      string.getObject()->length());
}

HnBool
HnStringObj::endsWith(const char *suffix, int n) const
{
    if ( n > len ) {
	return HnFALSE;
    }
    else {
	return (strncmp(chars() + len - n, suffix, n) == 0);
    }
}

HnBool
HnStringObj::endsWith(const char *suffix) const
{
    return endsWith(suffix, strlen(suffix));
}

HnBool
HnStringObj::endsWith(const HnString &string) const
{
    return endsWith(string.getObject()->chars(),
		    string.getObject()->length());
}

int
HnStringObj::indexOf(int ch, int fromIndex) const
{
    for ( int i=fromIndex; i<len; i++ ) {
	if ( buffer[i] == ch ) {
	    return i;
	}
    }

    return -1;
}

int
HnStringObj::indexOf(int ch) const
{
    return indexOf(ch, 0);
}

int
HnStringObj::lastIndexOf(int ch, int fromIndex) const
{
    for ( int i=fromIndex; i>=0; i-- ) {
	if ( buffer[i] == ch ) {
	    return i;
	}
    }

    return -1;
}

int
HnStringObj::lastIndexOf(int ch) const
{
    return lastIndexOf(ch, length() - 1);
}

HnString
HnStringObj::substring(int beginIndex, int endIndex) const
{
    /*
     * NOTE:
     *	    `beginIndex' is inclusive, while `endIndex' is exclusive.
     */

    if ( beginIndex < 0 || endIndex > len ) {
	HnAbort("HnStringObj::substring: index is out of bounds.");
    }

    return new_HnString(buffer + beginIndex, endIndex - beginIndex);
}

HnString
HnStringObj::substring(int beginIndex) const
{
    return substring(beginIndex, length());
}

HnString
HnStringObj::trim(void) const
{
    int beginIndex, endIndex;

    beginIndex = 0;
    while ( beginIndex < len && isspace(buffer[beginIndex]) ) {
	beginIndex ++;
    }

    endIndex = len;
    while ( endIndex > 0 && isspace(buffer[endIndex - 1]) ) {
	endIndex --;
    }

    return substring(beginIndex, endIndex);
}

/*
 * HnString
 */

HnString::HnString(const char *ptr)
{
    if ( ptr == NULL ) {
	assign(HnString::null);
    }
    else {
	assign(new_HnString(ptr, strlen(ptr)));
    }
}

HnString &
HnString::operator += (const char *ptr)
{
    assign(concat(ptr));
    return *this;
}

HnString &
HnString::operator += (const HnString &value)
{
    assign(concat(value.chars(), value.length()));
    return *this;
}

HnString &
HnString::operator += (char c)
{
    assign(concat(&c, 1));
    return *this;
}

HnString &
HnString::operator += (int value)
{
    assign(concat(valueOf(value)));
    return *this;
}

HnString &
HnString::operator += (double value)
{
    assign(concat(valueOf(value)));
    return *this;
}

void
HnString::append(const char *ptr, int n)
{
    assign(concat(ptr, n));
}

void
HnString::append(const char *ptr)
{
    append(ptr, ((ptr == NULL) ? 0 : strlen(ptr)));
}

void
HnString::append(const HnString &string)
{
    append(string.chars(), string.length());
}

#define FORMAT_ARRAY_SIZE	4096
#define FORMAT_MAX_BUFFER_SIZE  65536

HnString
HnString::format(const char *format, ... )
{
    va_list ap;
    char array[FORMAT_ARRAY_SIZE];
    int code;
    HnString string;

    va_start(ap, format);
    code = vsnprintf(array, sizeof(array), format, ap);
    va_end(ap);

    if ( code < (int)sizeof(array) - 1 ) {
	string = new_HnString(array);
    }
    else {
	char *buffer;
	int bufferSize;

	bufferSize = strlen(format) * 2;
	if ( (buffer = (char *)malloc(bufferSize)) == NULL ) {
	    HnSysError("malloc");
	}

	for ( ;; ) {
	    printf("bufferSize=%d\n", bufferSize);

	    va_start(ap, format);
	    code = vsnprintf(buffer, bufferSize, format, ap);

	    if ( code < bufferSize - 1 ) {
		break;
	    }

	    bufferSize *= 2;

	    if ( bufferSize > FORMAT_MAX_BUFFER_SIZE ) {
		HnAbort("string is too long.");
	    }

	    if ( (buffer = (char *)realloc(buffer, bufferSize)) == NULL ) {
		HnSysError("realloc");
	    }
	}

	string = new_HnString(buffer);

	free(buffer);
    }

    return string;
}

/*
 * Class Methods
 */

HnString
HnStringObj::getString(FILE *fp)
{
    HnString string = new_HnString();
    char buffer[4096];
    int length;

    while ( fgets(buffer, sizeof(buffer), fp) != NULL ) {
	length = strlen(buffer);
	if ( buffer[length - 1] == '\n' ) {
	    buffer[length - 1] = 0;
	    string.append(buffer, length - 1);
	    return string;
	}
	else {
	    string.append(buffer, length);
	}
    }

    if ( feof(fp) && string.length() == 0 ) {
	return HnString::null;
    }
    else {
	return string;
    }
}

HnStringVector
HnStringObj::sort(const HnStringVector &strings)
{
    HnStringVector vector = new_HnStringVector(strings);
    int i, j, len = strings.size();

    for ( i=0; i<len; i++ ) {
	for ( j=i+1; j<len; j++ ) {
	    if ( strcmp(vector[i], vector[j]) > 0 ) {
		vector.swapElementsAt(i, j);
	    }
	}
    }

    return vector;
}

/*
 * valueOf
 */

HnString
HnStringObj::valueOf(double value)
{
    return valueOf(value, 0, 6);
}

HnString
HnStringObj::valueOf(double value, int ndigit, int trailing)
{
    char buffer[256];

    sprintf(buffer, "%*.*g", ndigit, trailing, value);

    return (HnString)buffer;
}

int
HnStringObj::parseInt(const HnString &string)
{
    const char *ptr = string.chars();
    int value;
    char *endptr;

    value = (int)strtol(ptr, &endptr, 10);

    if ( endptr - ptr != string.length() ) {
	HnAbort("invalid string for an integer `%s'.",
		(char *)string);
    }

    return value;
}

double
HnStringObj::parseDouble(const HnString &string)
{
    const char *ptr = string.chars();

⌨️ 快捷键说明

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