这个也主要给自己看的,记录一下语法用的,当然也可以参考
文件String.h
#ifndef String_h
#define String_h
#include <stdio.h>
typedef struct{
long length;
char *strArr;
}String;
String *allocAndInit(void);
String *initWithString(const String *str);
long length(const String *str);
String *upper(String *str);
String *lower(String *str);
String *appendString(String *des,const String *src);
char charAt(long index,const String *str);
void freeString(String *str);
String *subString(const String *str,long index,long length);
int containsString(const String *str1,const String *partten);
int isEqual(const String *str1,const String *str2);
#endif /* String_h */
文件String.c
#include "String.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
String *allocAndInit(void){
String *str = (String *)malloc(sizeof(String));
memset(str, 0, sizeof(String));
char *arr = (char *)malloc(sizeof(char));
arr[0] = '\0';
str->strArr = arr;
return str;
}
String *initWithString(const String *str){
if (str == NULL) return NULL;
String *newStr = allocAndInit();
newStr->length = str->length;
char *arr = (char *)malloc(str->length+1);
strcpy(arr, str->strArr);
newStr->strArr = arr;
return newStr;
}
long length(const String *str){
if (str == NULL) return 0;
return str->length;
}
String *upper(String *str){
if (str == NULL) return str;
char *tmp = str->strArr;
for (int i = 0; i < str->length; i++) {
if (tmp[i] < 'a' || tmp[i] > 'z') continue;
tmp[i] += 32;
}
return str;
}
String *lower(String *str){
if (str == NULL) return str;
char *tmp = str->strArr;
for (int i = 0; i < str->length; i++) {
if (tmp[i] < 'A' || tmp[i] > 'Z') continue;
tmp[i] -= 32;
}
return str;
}
String *appendString(String *des,const String *src){
if (src == NULL || src->length <= 0) return des;
long len = des->length+src->length+1;
char *tmp = malloc(sizeof(char)*len);
strcpy(tmp, des->strArr);
strcat(tmp, src->strArr);
free(des->strArr);
des->length = len-1;
des->strArr = tmp;
return des;
}
char charAt(long index,const String *str){
assert(index > 0 || index < str->length);
if (str->strArr == NULL) return '\0';
char *arr = str->strArr;
return arr[index];
}
void freeString(String *str){
if (str == NULL) return;
if (str->strArr == NULL) return;
free(str->strArr);
free(str);
str = NULL;
}
String *subString(const String *str,long index,long length){
if (str == NULL) return NULL;
assert(index > 0 || index < str->length);
if (index + length > str->length) {
length = str->length - index;
}
String *subStr = allocAndInit();
subStr->length = length;
subStr->strArr = (char *)malloc(sizeof(char)*(length+1));
for (long i = 0,j = index; i < length; i++,j++) {
(subStr->strArr)[i] = (str->strArr)[j];
}
return subStr;
}
//这部分算法写的好蠢,凑合着看吧(可能会有问题😄)
int containsString(const String *str1,const String *partten){
if (str1 == NULL || partten == NULL) return 0;
if (partten->length > str1->length) return 0;
for (int i = 0; i < str1->length; i++) {
for (int j = 0; j < partten->length; j++) {
if (i+j>=str1->length) return 0;
int res1 = (partten->strArr)[j] == (str1->strArr)[i+j];
if(res1 == 0) break;
int res2 = j == partten->length-1;
if(res1 == res2) return 1;
}
}
return 0;
}
int isEqual(const String *str1,const String *str2){
if(str1 == NULL && str2 == NULL) return 1;
if (str1->length != str2->length) return 0;
int res = strcmp(str1->strArr, str2->strArr);
return res;
}
对接OC的类
GYJString.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface GYJString : NSObject
- (instancetype)init;
- (instancetype)initWithString:(GYJString *)string;
- (NSInteger)length;
- (void)upper;
- (void)lower;
- (void)appendString:(GYJString *)string;
- (char)charAt:(NSInteger)index;
- (GYJString *)subString:(NSInteger)index length:(NSInteger)length;
- (BOOL)containsString:(GYJString *)string;
- (BOOL)isEqual:(GYJString *)string;
- (GYJString *)copy;
- (GYJString *)mutableCopy;
@end
NS_ASSUME_NONNULL_END
.m实现
#import "GYJString.h"
#import "String.h"
@interface GYJString ()
{
String *_str;
}
@end
@implementation GYJString
- (instancetype)init{
if (self = [super init]) {
_str = allocAndInit();
}
return self;
}
- (instancetype)initWithString:(GYJString *)string{
if (self = [super init]) {
_str = initWithString(string->_str);
}
return self;
}
- (instancetype)initWithCString:(String *)str{
if (self = [super init]) {
_str = initWithString(str);
}
return self;
}
- (NSInteger)length{
return length(_str);
}
- (void)upper{
upper(_str);
}
- (void)lower{
lower(_str);
}
- (void)appendString:(GYJString *)string{
appendString(self->_str, string->_str);
}
- (char)charAt:(NSInteger)index{
return charAt(index, _str);
}
- (GYJString *)subString:(NSInteger)index length:(NSInteger)length{
String *tmp = subString(_str, index, length);
return [[GYJString alloc]initWithCString:tmp];
}
- (BOOL)containsString:(GYJString *)string{
return (BOOL)containsString(self->_str,string->_str);
}
- (BOOL)isEqual:(GYJString *)string{
return (BOOL)isEqual(self->_str, string->_str);
}
- (GYJString *)copy{
GYJString *p = self;
return p;
}
- (GYJString *)mutableCopy{
GYJString *tmp = [[GYJString alloc]initWithString:self];
return tmp;
}
- (void)dealloc{
deleteString(_str);
}
@end