Permalink
Browse files

removed the previous bytestring code and now use the new one

  • Loading branch information...
1 parent bdd85ed commit 28f110be48d8cc429625bc646a9ffcce3934a175 @lrz lrz committed Mar 16, 2009
Showing with 623 additions and 1,353 deletions.
  1. +13 −16 array.c
  2. +3 −6 bignum.c
  3. +4 −4 dir.c
  4. +3 −2 encoding.c
  5. +44 −522 file.c
  6. +6 −2 hash.c
  7. +1 −0 id.c
  8. +1 −0 id.h
  9. +1 −3 include/ruby/intern.h
  10. +3 −14 include/ruby/ruby.h
  11. +20 −11 io.c
  12. +2 −1 marshal.c
  13. +1 −31 numeric.c
  14. +27 −1 object.c
  15. +167 −177 pack.c
  16. +15 −117 parse.y
  17. +8 −30 re.c
  18. +23 −10 ruby.c
  19. +276 −403 string.c
  20. +5 −3 time.c
View
29 array.c
@@ -45,13 +45,16 @@ rb_ary_modify_check(VALUE ary)
#endif
if (mask == 0) {
bool _CFArrayIsMutable(void *);
- if (!_CFArrayIsMutable((void *)ary))
+ if (!_CFArrayIsMutable((void *)ary)) {
mask |= FL_FREEZE;
+ }
}
- if ((mask & FL_FREEZE) == FL_FREEZE)
+ if ((mask & FL_FREEZE) == FL_FREEZE) {
rb_raise(rb_eRuntimeError, "can't modify frozen/immutable array");
- if ((mask & FL_TAINT) == FL_TAINT && rb_safe_level() >= 4)
+ }
+ if ((mask & FL_TAINT) == FL_TAINT && rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "Insecure: can't modify array");
+ }
}
#define rb_ary_modify rb_ary_modify_check
@@ -594,9 +597,10 @@ rb_ary_unshift_m(VALUE ary, SEL sel, int argc, VALUE *argv)
return ary;
}
rb_ary_modify(ary);
- for (i = argc - 1; i >= 0; i--)
+ for (i = argc - 1; i >= 0; i--) {
CFArrayInsertValueAtIndex((CFMutableArrayRef)ary,
0, (const void *)RB2OC(argv[i]));
+ }
return ary;
}
@@ -3033,8 +3037,6 @@ rb_ary_cycle(VALUE ary, SEL sel, int argc, VALUE *argv)
return Qnil;
}
-#define tmpbuf(n, size) rb_str_tmp_new((n)*(size))
-
/*
* Recursively compute permutations of r elements of the set [0..n-1].
* When we have a complete permutation of array indexes, copy the values
@@ -3124,10 +3126,8 @@ rb_ary_permutation(VALUE ary, SEL sel, int argc, VALUE *argv)
}
}
else { /* this is the general case */
- volatile VALUE t0 = tmpbuf(n,sizeof(long));
- long *p = (long*)RSTRING_BYTEPTR(t0);
- volatile VALUE t1 = tmpbuf(n,sizeof(int));
- int *used = (int*)RSTRING_BYTEPTR(t1);
+ long *p = (long *)alloca(n * sizeof(long));
+ int *used = (int *)alloca(n * sizeof(int));
VALUE ary0 = rb_ary_dup(ary);
for (i = 0; i < n; i++) used[i] = 0; /* initialize array */
@@ -3202,8 +3202,7 @@ rb_ary_combination(VALUE ary, SEL sel, VALUE num)
}
}
else {
- volatile VALUE t0 = tmpbuf(n+1, sizeof(long));
- long *stack = (long*)RSTRING_BYTEPTR(t0);
+ long *stack = (long *)alloca((n + 1) * sizeof(long));
long nlen = combi_len(len, n);
volatile VALUE cc = rb_ary_new2(n);
long lev = 0;
@@ -3246,10 +3245,8 @@ static VALUE
rb_ary_product(VALUE ary, SEL sel, int argc, VALUE *argv)
{
int n = argc+1; /* How many arrays we're operating on */
- volatile VALUE t0 = tmpbuf(n, sizeof(VALUE));
- volatile VALUE t1 = tmpbuf(n, sizeof(int));
- VALUE *arrays = (VALUE*)RSTRING_BYTEPTR(t0); /* The arrays we're computing the product of */
- int *counters = (int*)RSTRING_BYTEPTR(t1); /* The current position in each one */
+ VALUE *arrays = (VALUE *)alloca(n * sizeof(VALUE));; /* The arrays we're computing the product of */
+ int *counters = (int *)alloca(n * sizeof(int)); /* The current position in each one */
VALUE result; /* The array we'll be returning */
long i,j;
long resultlen = 1;
View
9 bignum.c
@@ -939,7 +939,7 @@ VALUE
rb_big2str0(VALUE x, int base, int trim)
{
int off;
- VALUE ss, xx;
+ VALUE xx;
long n1, n2, len, hbase;
char* ptr;
@@ -955,8 +955,7 @@ rb_big2str0(VALUE x, int base, int trim)
n2 = big2str_find_n1(x, base);
n1 = (n2 + 1) / 2;
- ss = rb_usascii_str_new(0, n2 + 1); /* plus one for sign */
- ptr = RSTRING_BYTEPTR(ss); /* ok */
+ ptr = (char *)alloca(n2 + 1); /* plus one for sign */
ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-';
hbase = base*base;
@@ -975,10 +974,8 @@ rb_big2str0(VALUE x, int base, int trim)
}
ptr[len] = '\0';
- rb_str_resize(ss, len);
- RSTRING_SYNC(ss);
- return ss;
+ return rb_str_new2(ptr);
}
VALUE
View
8 dir.c
@@ -464,10 +464,10 @@ dir_inspect(VALUE dir, SEL sel)
if (dirp->path) {
const char *c = rb_obj_classname(dir);
int len = strlen(c) + strlen(dirp->path) + 4;
- VALUE s = rb_str_new(0, len);
- snprintf(RSTRING_BYTEPTR(s), len+1, "#<%s:%s>", c, dirp->path);
- RSTRING_SYNC(s);
- return s;
+
+ char *buf = (char *)alloca(len);
+ snprintf(buf, len, "#<%s:%s>", c, dirp->path);
+ return rb_str_new2(buf);
}
return rb_funcall(dir, rb_intern("to_s"), 0, 0);
}
View
5 encoding.c
@@ -288,8 +288,9 @@ static VALUE
enc_find(VALUE klass, SEL sel, VALUE enc)
{
VALUE e = enc_find2(enc);
- if (e == Qnil)
- rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_BYTEPTR(enc));
+ if (e == Qnil) {
+ rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
+ }
return e;
}
View
566 file.c
@@ -96,9 +96,13 @@ rb_get_path_check(VALUE obj, int check)
VALUE tmp;
static ID to_path;
- if (check) rb_check_safe_obj(obj);
+ if (check) {
+ rb_check_safe_obj(obj);
+ }
tmp = rb_check_string_type(obj);
- if (!NIL_P(tmp)) goto exit;
+ if (!NIL_P(tmp)) {
+ goto exit;
+ }
if (!to_path) {
to_path = rb_intern("to_path");
@@ -918,35 +922,40 @@ eaccess(const char *path, int mode)
struct stat st;
rb_uid_t euid;
- if (stat(path, &st) < 0) return -1;
+ if (stat(path, &st) < 0) {
+ return -1;
+ }
euid = geteuid();
if (euid == 0) {
/* Root can read or write any file. */
- if (!(mode & X_OK))
+ if (!(mode & X_OK)) {
return 0;
+ }
/* Root can execute any file that has any one of the execute
bits set. */
- if (st.st_mode & S_IXUGO)
+ if (st.st_mode & S_IXUGO) {
return 0;
+ }
return -1;
}
- if (st.st_uid == euid) /* owner */
+ if (st.st_uid == euid) { /* owner */
mode <<= 6;
- else if (group_member(st.st_gid))
+ }
+ else if (group_member(st.st_gid)) {
mode <<= 3;
+ }
- if ((st.st_mode & mode) == mode) return 0;
+ if ((st.st_mode & mode) == mode) {
+ return 0;
+ }
return -1;
#else
-# if defined(_MSC_VER) || defined(__MINGW32__)
- mode &= ~1;
-# endif
return access(path, mode);
#endif
}
@@ -1533,47 +1542,12 @@ rb_file_sticky_p(VALUE obj, SEL sel, VALUE fname)
static VALUE
rb_file_identical_p(VALUE obj, SEL sel, VALUE fname1, VALUE fname2)
{
-#ifndef DOSISH
struct stat st1, st2;
if (rb_stat(fname1, &st1) < 0) return Qfalse;
if (rb_stat(fname2, &st2) < 0) return Qfalse;
if (st1.st_dev != st2.st_dev) return Qfalse;
if (st1.st_ino != st2.st_ino) return Qfalse;
-#else
-#ifdef _WIN32
- BY_HANDLE_FILE_INFORMATION st1, st2;
- HANDLE f1 = 0, f2 = 0;
-#endif
-
- rb_secure(2);
-#ifdef _WIN32
- f1 = w32_io_info(&fname1, &st1);
- if (f1 == INVALID_HANDLE_VALUE) return Qfalse;
- f2 = w32_io_info(&fname2, &st2);
- if (f1) CloseHandle(f1);
- if (f2 == INVALID_HANDLE_VALUE) return Qfalse;
- if (f2) CloseHandle(f2);
-
- if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber &&
- st1.nFileIndexHigh == st2.nFileIndexHigh &&
- st1.nFileIndexLow == st2.nFileIndexLow)
- return Qtrue;
- if (!f1 || !f2) return Qfalse;
- if (rb_w32_iswin95()) return Qfalse;
-#else
- FilePathValue(fname1);
- fname1 = rb_str_new4(fname1);
- FilePathValue(fname2);
- if (access(RSTRING_PTR(fname1), 0)) return Qfalse;
- if (access(RSTRING_PTR(fname2), 0)) return Qfalse;
-#endif
- fname1 = rb_file_expand_path(fname1, Qnil);
- fname2 = rb_file_expand_path(fname2, Qnil);
- if (RSTRING_LEN(fname1) != RSTRING_LEN(fname2)) return Qfalse;
- if (rb_memcicmp(RSTRING_PTR(fname1), RSTRING_PTR(fname2), RSTRING_LEN(fname1)))
- return Qfalse;
-#endif
return Qtrue;
}
@@ -2337,22 +2311,7 @@ rb_file_s_rename(VALUE klass, SEL sel, VALUE from, VALUE to)
FilePathValue(to);
src = StringValueCStr(from);
dst = StringValueCStr(to);
-#if defined __CYGWIN__
- errno = 0;
-#endif
if (rename(src, dst) < 0) {
-#if defined DOSISH && !defined _WIN32
- switch (errno) {
- case EEXIST:
-#if defined (__EMX__)
- case EACCES:
-#endif
- if (chmod(dst, 0666) == 0 &&
- unlink(dst) == 0 &&
- rename(src, dst) == 0)
- return INT2FIX(0);
- }
-#endif
sys_fail2(from, to);
}
@@ -2393,89 +2352,23 @@ rb_file_s_umask(VALUE rcv, SEL sel, int argc, VALUE *argv)
return INT2FIX(omask);
}
-#ifdef __CYGWIN__
-#undef DOSISH
-#endif
-#if defined __CYGWIN__ || defined DOSISH
-#define DOSISH_UNC
-#define DOSISH_DRIVE_LETTER
-#define isdirsep(x) ((x) == '/' || (x) == '\\')
-#else
#define isdirsep(x) ((x) == '/')
-#endif
-#if defined _WIN32 || defined __CYGWIN__
-#define USE_NTFS 1
-#else
-#define USE_NTFS 0
-#endif
-
-#if USE_NTFS
-#define istrailinggabage(x) ((x) == '.' || (x) == ' ')
-#else
#define istrailinggabage(x) 0
-#endif
-
-#ifndef CharNext /* defined as CharNext[AW] on Windows. */
-# if defined(DJGPP)
-# define CharNext(p) ((p) + mblen(p, RUBY_MBCHAR_MAXSIZE))
-# else
-# define CharNext(p) ((p) + 1)
-# endif
-#endif
-#ifdef DOSISH_DRIVE_LETTER
-static inline int
-has_drive_letter(const char *buf)
-{
- if (ISALPHA(buf[0]) && buf[1] == ':') {
- return 1;
- }
- else {
- return 0;
- }
-}
-
-static char*
-getcwdofdrv(int drv)
-{
- char drive[4];
- char *drvcwd, *oldcwd;
-
- drive[0] = drv;
- drive[1] = ':';
- drive[2] = '\0';
-
- /* the only way that I know to get the current directory
- of a particular drive is to change chdir() to that drive,
- so save the old cwd before chdir()
- */
- oldcwd = my_getcwd();
- if (chdir(drive) == 0) {
- drvcwd = my_getcwd();
- chdir(oldcwd);
- free(oldcwd);
- }
- else {
- /* perhaps the drive is not exist. we return only drive letter */
- drvcwd = strdup(drive);
- }
- return drvcwd;
-}
+#ifndef CharNext
+# define CharNext(p) ((p) + 1)
#endif
static inline char *
skiproot(const char *path)
{
-#ifdef DOSISH_DRIVE_LETTER
- if (has_drive_letter(path)) path += 2;
-#endif
while (isdirsep(*path)) path++;
return (char *)path;
}
#define nextdirsep rb_path_next
-char *
+static char *
rb_path_next(const char *s)
{
while (*s && !isdirsep(*s)) {
@@ -2484,34 +2377,10 @@ rb_path_next(const char *s)
return (char *)s;
}
-#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
-#define skipprefix rb_path_skip_prefix
-#else
#define skipprefix(path) (path)
-#endif
-char *
-rb_path_skip_prefix(const char *path)
-{
-#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
-#ifdef DOSISH_UNC
- if (isdirsep(path[0]) && isdirsep(path[1])) {
- path += 2;
- while (isdirsep(*path)) path++;
- if (*(path = nextdirsep(path)) && path[1] && !isdirsep(path[1]))
- path = nextdirsep(path + 1);
- return (char *)path;
- }
-#endif
-#ifdef DOSISH_DRIVE_LETTER
- if (has_drive_letter(path))
- return (char *)(path + 2);
-#endif
-#endif
- return (char *)path;
-}
#define strrdirsep rb_path_last_separator
-char *
+static char *
rb_path_last_separator(const char *path)
{
char *last = NULL;
@@ -2552,54 +2421,23 @@ rb_path_end(const char *path)
return chompdirsep(path);
}
-#if USE_NTFS
-static char *
-ntfs_tail(const char *path)
-{
- while (*path == '.') path++;
- while (*path && *path != ':') {
- if (istrailinggabage(*path)) {
- const char *last = path++;
- while (istrailinggabage(*path)) path++;
- if (!*path || *path == ':') return (char *)last;
- }
- else if (isdirsep(*path)) {
- const char *last = path++;
- while (isdirsep(*path)) path++;
- if (!*path) return (char *)last;
- if (*path == ':') path++;
- }
- else {
- path = CharNext(path);
- }
- }
- return (char *)path;
-}
-#endif
-
#define BUFCHECK(cond) do {\
long bdiff = p - buf;\
if (cond) {\
do {buflen *= 2;} while (cond);\
- rb_str_resize(result, buflen);\
- buf = RSTRING_BYTEPTR(result);\
+ rb_bytestring_resize(result, buflen);\
+ buf = (char *)rb_bytestring_byte_pointer(result);\
p = buf + bdiff;\
pend = buf + buflen;\
}\
} while (0)
#define BUFINIT() (\
- p = buf = RSTRING_BYTEPTR(result),\
- buflen = RSTRING_BYTELEN(result),\
+ p = buf = (char *)rb_bytestring_byte_pointer(result),\
+ buflen = rb_bytestring_length(result),\
pend = p + buflen)
-#if WITH_OBJC
-# define SET_EXTERNAL_ENCODING()
-#else
-# define SET_EXTERNAL_ENCODING() (\
- (void)(extenc || (extenc = rb_default_external_encoding())),\
- rb_enc_associate(result, extenc))
-#endif
+#define SET_EXTERNAL_ENCODING()
static int is_absolute_path(const char*);
@@ -2610,9 +2448,6 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
char *buf, *p, *pend, *root;
long buflen, dirlen;
int tainted;
-#if !WITH_OBJC
- rb_encoding *extenc = 0;
-#endif
FilePathValue(fname);
s = StringValuePtr(fname);
@@ -2629,15 +2464,7 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
dirlen = strlen(dir);
BUFCHECK(dirlen > buflen);
strcpy(buf, dir);
-#if defined DOSISH || defined __CYGWIN__
- for (p = buf; *p; p = CharNext(p)) {
- if (*p == '\\') {
- *p = '/';
- }
- }
-#else
p = buf + strlen(dir);
-#endif
s++;
tainted = 1;
SET_EXTERNAL_ENCODING();
@@ -2666,52 +2493,13 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
#endif
}
}
-#ifdef DOSISH_DRIVE_LETTER
- /* skip drive letter */
- else if (has_drive_letter(s)) {
- if (isdirsep(s[2])) {
- /* specified drive letter, and full path */
- /* skip drive letter */
- BUFCHECK(bdiff + 2 >= buflen);
- memcpy(p, s, 2);
- p += 2;
- s += 2;
- }
- else {
- /* specified drive, but not full path */
- int same = 0;
- if (!NIL_P(dname)) {
- file_expand_path(dname, Qnil, result);
- BUFINIT();
- if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
- /* ok, same drive */
- same = 1;
- }
- }
- if (!same) {
- char *dir = getcwdofdrv(*s);
-
- tainted = 1;
- dirlen = strlen(dir);
- BUFCHECK(dirlen > buflen);
- strcpy(buf, dir);
- free(dir);
- SET_EXTERNAL_ENCODING();
- }
- p = chompdirsep(skiproot(buf));
- s += 2;
- }
- }
-#endif
else if (!is_absolute_path(s)) {
if (!NIL_P(dname)) {
long n;
file_expand_path(dname, Qnil, result);
BUFINIT();
-#if WITH_OBJC
n = RSTRING_LEN(result);
BUFCHECK(n + 2 > buflen);
-#endif
}
else {
char *dir = my_getcwd();
@@ -2723,15 +2511,7 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
xfree(dir);
SET_EXTERNAL_ENCODING();
}
-#if defined DOSISH || defined __CYGWIN__
- if (isdirsep(*s)) {
- /* specified full path, but not drive letter nor UNC */
- /* we need to get the drive letter or UNC share name */
- p = skipprefix(buf);
- }
- else
-#endif
- p = chompdirsep(skiproot(buf));
+ p = chompdirsep(skiproot(buf));
}
else {
b = s;
@@ -2773,41 +2553,17 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
}
b = ++s;
}
-#if USE_NTFS
- else {
- do *++s; while (istrailinggabage(*s));
- }
-#endif
break;
case '/':
-#if defined DOSISH || defined __CYGWIN__
- case '\\':
-#endif
b = ++s;
break;
default:
/* ordinary path element, beginning don't move */
break;
}
}
-#if USE_NTFS
- else {
- --s;
- case ' ': {
- const char *e = s;
- while (istrailinggabage(*s)) s++;
- if (!*s) {
- s = e;
- goto endpath;
- }
- }
- }
-#endif
break;
case '/':
-#if defined DOSISH || defined __CYGWIN__
- case '\\':
-#endif
if (s > b) {
long rootdiff = root - buf;
BUFCHECK(bdiff + (s-b+1) >= buflen);
@@ -2825,90 +2581,25 @@ file_expand_path(VALUE fname, VALUE dname, VALUE result)
}
if (s > b) {
-#if USE_NTFS
- endpath:
- if (s > b + 6 && strncasecmp(s - 6, ":$DATA", 6) == 0) {
- /* alias of stream */
- /* get rid of a bug of x64 VC++ */
- if (*(s-7) == ':') s -= 7; /* prime */
- else if (memchr(b, ':', s - 6 - b)) s -= 6; /* alternative */
- }
-#endif
BUFCHECK(bdiff + (s-b) >= buflen);
memcpy(++p, b, s-b);
p += s-b;
}
if (p == skiproot(buf) - 1) p++;
-#if USE_NTFS
- *p = '\0';
- if (1 &&
-#ifdef __CYGWIN__
- !(buf[0] == '/' && !buf[1]) &&
-#endif
- !strpbrk(b = buf, "*?")) {
- size_t len;
- WIN32_FIND_DATA wfd;
-#ifdef __CYGWIN__
- int lnk_added = 0, is_symlink = 0;
- struct stat st;
- char w32buf[MAXPATHLEN], sep = 0;
- p = 0;
- if (lstat(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
- is_symlink = 1;
- p = strrdirsep(buf);
- if (!p) p = skipprefix(buf);
- if (p) {
- sep = *p;
- *p = '\0';
- }
- }
- if (cygwin_conv_to_win32_path(buf, w32buf) == 0) {
- b = w32buf;
- }
- if (p) *p = sep;
- else p = buf;
- if (is_symlink && b == w32buf) {
- len = strlen(p);
- if (len > 4 && STRCASECMP(p + len - 4, ".lnk") != 0) {
- lnk_added = 1;
- strlcat(w32buf, ".lnk", sizeof(w32buf));
- }
- }
-#endif
- HANDLE h = FindFirstFile(b, &wfd);
- if (h != INVALID_HANDLE_VALUE) {
- FindClose(h);
- p = strrdirsep(buf);
- len = strlen(wfd.cFileName);
-#ifdef __CYGWIN__
- if (lnk_added && len > 4 &&
- STRCASECMP(wfd.cFileName + len - 4, ".lnk") == 0) {
- wfd.cFileName[len -= 4] = '\0';
- }
-#endif
- if (!p) p = buf;
- else ++p;
- BUFCHECK(bdiff + len >= buflen);
- memcpy(p, wfd.cFileName, len + 1);
- p += len;
- }
+ if (tainted) {
+ OBJ_TAINT(result);
}
-#endif
-
- if (tainted) OBJ_TAINT(result);
- rb_str_set_len(result, p - buf);
-#if !WITH_OBJC
- rb_enc_check(fname, result);
-#endif
- RSTRING_SYNC(result);
+ rb_bytestring_resize(result, p - buf);
return result;
}
VALUE
rb_file_expand_path(VALUE fname, VALUE dname)
{
- return file_expand_path(fname, dname, rb_usascii_str_new(0, MAXPATHLEN + 2));
+ VALUE bstr = rb_bytestring_new();
+ rb_bytestring_resize(bstr, MAXPATHLEN + 2);
+ return file_expand_path(fname, dname, bstr);
}
/*
@@ -2989,42 +2680,21 @@ rb_file_s_basename(VALUE rcv, SEL sel, int argc, VALUE *argv)
{
VALUE fname, fext, basename;
const char *name, *p;
-#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
- char *root;
-#endif
int f, n;
if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
StringValue(fext);
}
FilePathStringValue(fname);
- if (RSTRING_LEN(fname) == 0 || !*(name = RSTRING_PTR(fname)))
+ if (RSTRING_LEN(fname) == 0 || !*(name = RSTRING_PTR(fname))) {
return fname;
+ }
name = skipprefix(name);
-#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
- root = name;
-#endif
while (isdirsep(*name))
name++;
if (!*name) {
p = name - 1;
f = 1;
-#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
- if (name != root) {
- /* has slashes */
- }
-#ifdef DOSISH_DRIVE_LETTER
- else if (*p == ':') {
- p++;
- f = 0;
- }
-#endif
-#ifdef DOSISH_UNC
- else {
- p = "/";
- }
-#endif
-#endif
}
else {
if (!(p = strrdirsep(name))) {
@@ -3033,20 +2703,13 @@ rb_file_s_basename(VALUE rcv, SEL sel, int argc, VALUE *argv)
else {
while (isdirsep(*p)) p++; /* skip last / */
}
-#if USE_NTFS
- n = ntfs_tail(p) - p;
-#else
n = chompdirsep(p) - p;
-#endif
if (NIL_P(fext) || !(f = rmext(p, n, StringValueCStr(fext)))) {
f = n;
}
if (f == RSTRING_LEN(fname)) return fname;
}
basename = rb_str_new(p, f);
-#if !WITH_OBJC
- rb_enc_copy(basename, fname);
-#endif
OBJ_INFECT(basename, fname);
return basename;
}
@@ -3072,35 +2735,16 @@ rb_file_s_dirname(VALUE klass, SEL sel, VALUE fname)
FilePathStringValue(fname);
name = StringValueCStr(fname);
root = skiproot(name);
-#ifdef DOSISH_UNC
- if (root > name + 1 && isdirsep(*name))
- root = skipprefix(name = root - 2);
-#else
- if (root > name + 1)
+ if (root > name + 1) {
name = root - 1;
-#endif
+ }
p = strrdirsep(root);
if (!p) {
p = root;
}
if (p == name)
return rb_usascii_str_new2(".");
-#ifdef DOSISH_DRIVE_LETTER
- if (has_drive_letter(name) && isdirsep(*(name + 2))) {
- const char *top = skiproot(name + 2);
- dirname = rb_str_new(name, 3);
- rb_str_cat(dirname, top, p - top);
- }
- else
-#endif
dirname = rb_str_new(name, p - name);
-#ifdef DOSISH_DRIVE_LETTER
- if (has_drive_letter(name) && root == name + 2 && p - name == 2)
- rb_str_cat(dirname, ".", 1);
-#endif
-#if !WITH_OBJC
- rb_enc_copy(dirname, fname);
-#endif
OBJ_INFECT(dirname, fname);
return dirname;
}
@@ -3136,37 +2780,15 @@ rb_file_s_extname(VALUE klass, SEL sel, VALUE fname)
e = 0;
while (*p) {
if (*p == '.' || istrailinggabage(*p)) {
-#if USE_NTFS
- const char *last = p++, *dot = last;
- while (istrailinggabage(*p)) {
- if (*p == '.') dot = p;
- p++;
- }
- if (!*p || *p == ':') {
- p = last;
- break;
- }
- e = dot;
- continue;
-#else
e = p; /* get the last dot of the last component */
-#endif
- }
-#if USE_NTFS
- else if (*p == ':') {
- break;
}
-#endif
else if (isdirsep(*p))
break;
p = CharNext(p);
}
if (!e || e == name || e+1 == p) /* no dot, or the only dot is first or end? */
return rb_str_new(0, 0);
extname = rb_str_new(e, p - e); /* keep the dot, too! */
-#if !WITH_OBJC
- rb_enc_copy(extname, fname);
-#endif
OBJ_INFECT(extname, fname);
return extname;
}
@@ -3210,24 +2832,12 @@ static VALUE separator;
static VALUE rb_file_join(VALUE ary, VALUE sep);
-#if !WITH_OBJC
-static VALUE
-file_inspect_join(VALUE ary, VALUE argp, int recur)
-{
- VALUE *arg = (VALUE *)argp;
- if (recur) return rb_usascii_str_new2("[...]");
- return rb_file_join(arg[0], arg[1]);
-}
-#endif
-
static VALUE
rb_file_join(VALUE ary, VALUE sep)
{
-#if WITH_OBJC
- CFMutableStringRef mstr;
long count;
- mstr = CFStringCreateMutable(NULL, 0);
+ CFMutableStringRef mstr = CFStringCreateMutable(NULL, 0);
count = RARRAY_LEN(ary);
if (count > 0) {
@@ -3249,61 +2859,8 @@ rb_file_join(VALUE ary, VALUE sep)
}
}
CFMakeCollectable(mstr);
- return (VALUE)mstr;
-#else
- long len, i, count;
- VALUE result, tmp;
- char *name, *tail;
-
- if (RARRAY_LEN(ary) == 0) return rb_str_new(0, 0);
-
- len = 1;
- for (i=0, count=RARRAY_LEN(ary); i<count; i++) {
- if (TYPE(RARRAY_AT(ary, i)) == T_STRING) {
- len += RSTRING_BYTELEN(RARRAY_AT(ary, i));
- }
- else {
- len += 10;
- }
- }
- if (!NIL_P(sep)) {
- StringValue(sep);
- len += RSTRING_BYTELEN(sep) * RARRAY_LEN(ary) - 1;
- }
- result = rb_str_buf_new(len);
- OBJ_INFECT(result, ary);
- for (i=0; i<RARRAY_LEN(ary); i++) {
- tmp = RARRAY_AT(ary, i);
- switch (TYPE(tmp)) {
- case T_STRING:
- break;
- case T_ARRAY:
- {
- VALUE args[2];
-
- args[0] = tmp;
- args[1] = sep;
- tmp = rb_exec_recursive(file_inspect_join, ary, (VALUE)args);
- }
- break;
- default:
- FilePathStringValue(tmp);
- }
- name = StringValueCStr(result);
- if (i > 0 && !NIL_P(sep)) {
- tail = chompdirsep(name);
- if (RSTRING_BYTEPTR(tmp) && isdirsep(RSTRING_BYTEPTR(tmp)[0])) {
- rb_str_set_len(result, tail - name);
- }
- else if (!*tail) {
- rb_str_buf_append(result, sep);
- }
- }
- rb_str_buf_append(result, tmp);
- }
- return result;
-#endif
+ return (VALUE)mstr;
}
/*
@@ -4382,24 +3939,11 @@ rb_file_const(const char *name, VALUE value)
static int
is_absolute_path(const char *path)
{
-#ifdef DOSISH_DRIVE_LETTER
- if (has_drive_letter(path) && isdirsep(path[2])) return 1;
-#endif
-#ifdef DOSISH_UNC
- if (isdirsep(path[0]) && isdirsep(path[1])) return 1;
-#endif
-#ifndef DOSISH
- if (path[0] == '/') return 1;
-#endif
- return 0;
+ return path[0] == '/';
}
#ifndef ENABLE_PATH_CHECK
-# if defined DOSISH || defined __CYGWIN__
-# define ENABLE_PATH_CHECK 0
-# else
-# define ENABLE_PATH_CHECK 1
-# endif
+# define ENABLE_PATH_CHECK 1
#endif
#if ENABLE_PATH_CHECK
@@ -4482,15 +4026,6 @@ rb_path_check(const char *path)
return 1;
}
-#if defined(__MACOS__) || defined(riscos)
-static int
-is_macos_native_path(const char *path)
-{
- if (strchr(path, ':')) return 1;
- return 0;
-}
-#endif
-
static int
file_load_ok(const char *path)
{
@@ -4572,15 +4107,6 @@ rb_find_file(VALUE path)
f = StringValueCStr(path);
}
-#if defined(__MACOS__) || defined(riscos)
- if (is_macos_native_path(f)) {
- if (rb_safe_level() >= 1 && !fpath_check(f)) {
- rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
- }
- if (file_load_ok(f)) return path;
- }
-#endif
-
if (is_absolute_path(f)) {
if (rb_safe_level() >= 1 && !fpath_check(f)) {
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
@@ -4746,11 +4272,7 @@ Init_File(void)
rb_objc_define_method(rb_ccFile, "split", rb_file_s_split, 1);
rb_objc_define_method(rb_ccFile, "join", rb_file_s_join, -2);
-#ifdef DOSISH
- rb_define_const(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_usascii_str_new2("\\")));
-#else
rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil);
-#endif
rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP)));
rb_objc_define_method(rb_cIO, "stat", rb_io_stat, 0); /* this is IO's method */
View
8 hash.c
@@ -1144,7 +1144,9 @@ inspect_i(VALUE key, VALUE value, VALUE str)
{
VALUE str2;
- if (key == Qundef) return ST_CONTINUE;
+ if (key == Qundef) {
+ return ST_CONTINUE;
+ }
if (RSTRING_LEN(str) > 1) {
rb_str_cat2(str, ", ");
}
@@ -1162,7 +1164,9 @@ inspect_hash(VALUE hash, VALUE dummy, int recur)
{
VALUE str;
- if (recur) return rb_usascii_str_new2("{...}");
+ if (recur) {
+ return rb_usascii_str_new2("{...}");
+ }
str = rb_str_buf_new2("{");
rb_hash_foreach(hash, inspect_i, str);
rb_str_buf_cat2(str, "}");
View
1 id.c
@@ -65,6 +65,7 @@ Init_id(void)
selRespondTo = sel_registerName("respond_to?:");
selMethodMissing = sel_registerName("method_missing:");
selCopy = sel_registerName("copy");
+ selMutableCopy = sel_registerName("mutableCopy");
sel_ignored = sel_registerName("retain");
assert(sel_ignored == sel_registerName("release"));
sel_zone = sel_registerName("zone");
View
1 id.h
@@ -77,6 +77,7 @@ extern SEL selInitialize2;
extern SEL selRespondTo;
extern SEL selMethodMissing;
extern SEL selCopy;
+extern SEL selMutableCopy;
extern SEL selToS;
extern SEL sel_ignored;
extern SEL sel_zone;
View
4 include/ruby/intern.h
@@ -322,9 +322,7 @@ VALUE rb_file_expand_path(VALUE, VALUE);
void rb_file_const(const char*, VALUE);
int rb_find_file_ext(VALUE*, const char* const*);
VALUE rb_find_file(VALUE);
-char *rb_path_next(const char *);
-char *rb_path_skip_prefix(const char *);
-char *rb_path_last_separator(const char *);
+#define rb_path_skip_prefix(path) (path)
char *rb_path_end(const char *);
VALUE rb_file_directory_p(VALUE,SEL,VALUE);
/* gc.c */
View
17 include/ruby/ruby.h
@@ -360,6 +360,8 @@ VALUE rb_bytestring_new_with_data(UInt8 *buf, long size);
CFMutableDataRef rb_bytestring_wrapped_data(VALUE);
UInt8 *rb_bytestring_byte_pointer(VALUE);
VALUE rb_coerce_to_bytestring(VALUE);
+long rb_bytestring_length(VALUE str);
+void rb_bytestring_resize(VALUE str, long newsize);
void rb_check_safe_obj(VALUE);
void rb_check_safe_str(VALUE);
@@ -581,21 +583,8 @@ struct RString {
RSTRING(str)->as.ary : \
RSTRING(str)->as.heap.ptr)
#else
-/* IMPORTANT: try to avoid using RSTRING_PTR/RSTRING_LEN if necessary,
- * because they can be slow operations in non-8bit strings.
- * If you modify RSTRING_BYTEPTR, you need to call RSTRING_SYNC in order to
- * synchronize its content with the real string storage.
- * RSTRING_BYTEPTR/RSTRING_BYTELEN deal with bytes. If you want to access a C string
- * pointer, please use RSTRING_PTR/RSTRING/LEN instead which are faster.
- */
-char *rb_str_byteptr(VALUE);
-long rb_str_bytelen(VALUE);
-void rb_str_bytesync(VALUE);
const char *rb_str_cstr(VALUE);
long rb_str_clen(VALUE);
-# define RSTRING_BYTEPTR(str) (rb_str_byteptr((VALUE)str))
-# define RSTRING_BYTELEN(str) (rb_str_bytelen((VALUE)str))
-# define RSTRING_SYNC(str) (rb_str_bytesync((VALUE)str))
# define RSTRING_PTR(str) (rb_str_cstr((VALUE)str))
# define RSTRING_LEN(str) (rb_str_clen((VALUE)str))
#endif
@@ -814,7 +803,7 @@ struct RBignum {
#define SPECIAL_CONST_P(x) (IMMEDIATE_P(x) || !RTEST(x))
-#define FL_ABLE(x) (!SPECIAL_CONST_P(x) && BUILTIN_TYPE(x) != T_NODE)
+#define FL_ABLE(x) (!SPECIAL_CONST_P(x) && !NATIVE(x) && BUILTIN_TYPE(x) != T_NODE)
#define FL_TEST(x,f) (FL_ABLE(x)?(RBASIC(x)->flags&(f)):0)
#define FL_ANY(x,f) FL_TEST(x,f)
#define FL_ALL(x,f) (FL_TEST(x,f) == (f))
View
31 io.c
@@ -409,14 +409,22 @@ io_write(VALUE io, SEL sel, VALUE to_write)
length = CFDataGetLength(data);
}
else {
- buffer = (UInt8 *)CFStringGetCStringPtr((CFStringRef)to_write,
- kCFStringEncodingUTF8);
+ buffer = (UInt8 *)RSTRING_PTR(to_write);
if (buffer != NULL) {
- length = CFStringGetLength((CFStringRef)to_write);
+ length = RSTRING_LEN(to_write);
}
else {
- buffer = (UInt8 *)rb_str_byteptr(to_write);
- length = (CFIndex)rb_str_bytelen(to_write);
+ const long max = CFStringGetMaximumSizeForEncoding(
+ CFStringGetLength((CFStringRef)to_write),
+ kCFStringEncodingUTF8);
+
+ buffer = (UInt8 *)alloca(max + 1);
+ if (!CFStringGetCString((CFStringRef)to_write, (char *)buffer,
+ max, kCFStringEncodingUTF8)) {
+ // XXX what could we do?
+ abort();
+ }
+ length = strlen((char *)buffer);
}
}
@@ -2211,18 +2219,19 @@ VALUE rb_io_write(VALUE v, SEL sel, VALUE i)
*/
static VALUE
-rb_f_p(VALUE self, SEL sel, int argc, VALUE *argv) {
+rb_f_p(VALUE self, SEL sel, int argc, VALUE *argv)
+{
int i;
VALUE ret = Qnil;
- for (i=0; i<argc; i++) {
- rb_p(argv[i], (SEL)0);
+ for (i = 0; i < argc; i++) {
+ rb_p(argv[i], 0);
}
if (argc == 1) {
- ret = argv[0];
+ ret = argv[0];
}
else if (argc > 1) {
- ret = rb_ary_new4(argc, argv);
+ ret = rb_ary_new4(argc, argv);
}
return ret;
@@ -3401,7 +3410,7 @@ argf_inplace_mode_set(VALUE argf, SEL sel, VALUE val)
if (ruby_inplace_mode != NULL) {
free(ruby_inplace_mode);
}
- ruby_inplace_mode = strdup(RSTRING_BYTEPTR(val));
+ ruby_inplace_mode = strdup(RSTRING_PTR(val));
}
return argf;
}
View
3 marshal.c
@@ -945,7 +945,8 @@ marshal_dump(int argc, VALUE *argv)
GC_WB(&arg->str, port);
}
- RSTRING_BYTEPTR(arg->str); /* force bytestring creation */
+ // TODO should create ByteString
+ //RSTRING_BYTEPTR(arg->str); /* force bytestring creation */
GC_WB(&arg->symbols, st_init_numtable());
GC_WB(&arg->data, st_init_numtable());
View
32 numeric.c
@@ -19,10 +19,6 @@
#include <ruby/node.h>
#include "roxor.h"
-#if defined(__FreeBSD__) && __FreeBSD__ < 4
-#include <floatingpoint.h>
-#endif
-
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
@@ -87,17 +83,14 @@ round(double x)
static ID id_coerce, id_to_i, id_eq;
VALUE rb_cNumeric;
-#if WITH_OBJC
VALUE rb_cCFNumber;
-#endif
VALUE rb_cFloat;
VALUE rb_cInteger;
VALUE rb_cFixnum;
VALUE rb_eZeroDivError;
VALUE rb_eFloatDomainError;
-#if WITH_OBJC
static CFMutableDictionaryRef fixnum_dict = NULL;
static struct RFixnum *fixnum_cache = NULL;
@@ -134,7 +127,6 @@ rb_box_fixnum(VALUE fixnum)
return (VALUE)val;
}
-#endif
void
rb_num_zerodiv(void)
@@ -180,7 +172,7 @@ coerce_rescue(VALUE *x)
rb_raise(rb_eTypeError, "%s can't be coerced into %s",
rb_special_const_p(x[1])?
- RSTRING_BYTEPTR(v):
+ RSTRING_PTR(v):
rb_obj_classname(x[1]),
rb_obj_classname(x[0]));
return Qnil; /* dummy */
@@ -1940,19 +1932,13 @@ static VALUE
int_chr(VALUE num, SEL sel, int argc, VALUE *argv)
{
char c;
-#if !WITH_OBJC
- int n;
-#endif
long i = NUM2LONG(num);
rb_encoding *enc;
VALUE str;
switch (argc) {
case 0:
if (i < 0 || 0xff < i) {
-#if !WITH_OBJC
- out_of_range:
-#endif
rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
}
c = i;
@@ -1968,16 +1954,8 @@ int_chr(VALUE num, SEL sel, int argc, VALUE *argv)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
break;
}
-#if WITH_OBJC
enc = rb_to_encoding(argv[0]);
str = rb_enc_str_new(&c, 1, enc);
-#else
- enc = rb_to_encoding(argv[0]);
- if (!enc) enc = rb_ascii8bit_encoding();
- if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range;
- str = rb_enc_str_new(0, n, enc);
- rb_enc_mbcput(i, RSTRING_BYTEPTR(str), enc);
-#endif
return str;
}
@@ -3176,7 +3154,6 @@ fix_even_p(VALUE num, SEL sel)
return Qtrue;
}
-#if WITH_OBJC
static const char *
imp_rb_float_objCType(void *rcv, SEL sel)
{
@@ -3236,7 +3213,6 @@ rb_install_nsnumber_primitives(void)
rb_objc_install_method2(klass, "longValue",
(IMP)imp_rb_fixnum_longValue);
}
-#endif
void
Init_Numeric(void)
@@ -3247,17 +3223,13 @@ Init_Numeric(void)
rb_eZeroDivError = rb_define_class("ZeroDivisionError", rb_eStandardError);
rb_eFloatDomainError = rb_define_class("FloatDomainError", rb_eRangeError);
-#if WITH_OBJC
rb_cCFNumber = (VALUE)objc_getClass("NSCFNumber");
rb_cNumeric = rb_define_class("Numeric", (VALUE)objc_getClass("NSNumber"));
RCLASS_SET_VERSION_FLAG(rb_cNumeric, RCLASS_IS_OBJECT_SUBCLASS);
rb_define_object_special_methods(rb_cNumeric);
/* overriding NSObject methods */
rb_objc_define_method(rb_cNumeric, "class", rb_obj_class, 0);
rb_objc_define_method(rb_cNumeric, "dup", rb_obj_dup, 0);
-#else
- rb_cNumeric = rb_define_class("Numeric", rb_cObject);
-#endif
rb_objc_define_method(rb_cNumeric, "singleton_method_added", num_sadded, 1);
rb_include_module(rb_cNumeric, rb_mComparable);
@@ -3416,7 +3388,5 @@ Init_Numeric(void)
rb_objc_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
rb_objc_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0);
-#if WITH_OBJC
rb_install_nsnumber_primitives();
-#endif
}
View
28 object.c
@@ -771,6 +771,11 @@ rb_obj_tainted(VALUE obj)
if (!SPECIAL_CONST_P(obj) && NATIVE(obj)) {
switch (TYPE(obj)) {
case T_STRING:
+ if (*(VALUE *)obj == rb_cByteString) {
+ // TODO
+ return Qfalse;
+ }
+ // fall through
case T_ARRAY:
case T_HASH:
#ifdef __LP64__
@@ -780,8 +785,9 @@ rb_obj_tainted(VALUE obj)
return rb_objc_flag_check((const void *)obj, FL_TAINT) ? Qtrue : Qfalse;
}
}
- if (FL_TEST(obj, FL_TAINT))
+ if (FL_TEST(obj, FL_TAINT)) {
return Qtrue;
+ }
return Qfalse;
}
@@ -801,6 +807,11 @@ rb_obj_taint(VALUE obj)
if (!SPECIAL_CONST_P(obj) && NATIVE(obj)) {
switch (TYPE(obj)) {
case T_STRING:
+ if (*(VALUE *)obj == rb_cByteString) {
+ // TODO
+ return obj;
+ }
+ // fall through
case T_ARRAY:
case T_HASH:
#ifdef __LP64__
@@ -836,6 +847,11 @@ rb_obj_untaint(VALUE obj)
if (!SPECIAL_CONST_P(obj) && NATIVE(obj)) {
switch (TYPE(obj)) {
case T_STRING:
+ if (*(VALUE *)obj == rb_cByteString) {
+ // TODO
+ return obj;
+ }
+ // fall through
case T_ARRAY:
case T_HASH:
#ifdef __LP64__
@@ -901,6 +917,11 @@ rb_obj_freeze(VALUE obj)
else if (NATIVE(obj)) {
switch(TYPE(obj)) {
case T_STRING:
+ if (*(VALUE *)obj == rb_cByteString) {
+ // TODO
+ return obj;
+ }
+ // fall through
case T_ARRAY:
case T_HASH:
#ifdef __LP64__
@@ -942,6 +963,11 @@ rb_obj_frozen_p(VALUE obj)
}
switch (TYPE(obj)) {
case T_STRING:
+ if (*(VALUE *)obj == rb_cByteString) {
+ // TODO
+ return Qfalse;
+ }
+ // fall through
case T_ARRAY:
case T_HASH:
#ifdef __LP64__
View
344 pack.c
@@ -364,8 +364,8 @@ num2i32(VALUE x)
#endif
static const char toofew[] = "too few arguments";
-static void encodes(VALUE,const char*,long,int);
-static void qpencode(VALUE,VALUE,long);
+static void encodes(CFMutableDataRef,const char*,long,int);
+static void qpencode(CFMutableDataRef,VALUE,long);
static unsigned long utf8_to_uv(const char*,long*);
@@ -440,7 +440,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
static const char nul10[] = "\0\0\0\0\0\0\0\0\0\0";
static const char spc10[] = " ";
const char *p, *pend;
- VALUE res, from, associates = 0;
+ VALUE from, associates = 0;
char type;
long items, len, idx, plen;
const char *ptr;
@@ -451,11 +451,9 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
StringValue(fmt);
p = RSTRING_PTR(fmt);
pend = p + RSTRING_LEN(fmt);
- res = rb_str_buf_new(0);
-#if WITH_OBJC
- RSTRING_BYTEPTR(res); /* create bytestring */
-#endif
+ VALUE bres = rb_bytestring_new();
+ CFMutableDataRef data = rb_bytestring_wrapped_data(bres);
items = RARRAY_LEN(ary);
idx = 0;
@@ -521,9 +519,6 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
StringValue(from);
ptr = RSTRING_PTR(from);
plen = RSTRING_LEN(from);
-#if !WITH_OBJC
- OBJ_INFECT(res, from);
-#endif
}
if (p[-1] == '*')
@@ -534,18 +529,19 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
case 'A': /* arbitrary binary string (ASCII space padded) */
case 'Z': /* null terminated string */
if (plen >= len) {
- rb_str_buf_cat(res, ptr, len);
- if (p[-1] == '*' && type == 'Z')
- rb_str_buf_cat(res, nul10, 1);
+ CFDataAppendBytes(data, (const UInt8 *)ptr, len);
+ if (p[-1] == '*' && type == 'Z') {
+ CFDataAppendBytes(data, (const UInt8 *)nul10, 1);
+ }
}
else {
- rb_str_buf_cat(res, ptr, plen);
+ CFDataAppendBytes(data, (const UInt8 *)ptr, plen);
len -= plen;
while (len >= 10) {
- rb_str_buf_cat(res, (type == 'A')?spc10:nul10, 10);
+ CFDataAppendBytes(data, (const UInt8 *)((type == 'A')?spc10:nul10), 10);
len -= 10;
}
- rb_str_buf_cat(res, (type == 'A')?spc10:nul10, len);
+ CFDataAppendBytes(data, (const UInt8 *)((type == 'A')?spc10:nul10), len);
}
break;
@@ -565,15 +561,15 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
byte >>= 1;
else {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
byte = 0;
}
}
if (len & 7) {
char c;
byte >>= 7 - (len & 7);
c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
}
len = j;
goto grow;
@@ -595,15 +591,15 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
byte <<= 1;
else {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
byte = 0;
}
}
if (len & 7) {
char c;
byte <<= 7 - (len & 7);
c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
}
len = j;
goto grow;
@@ -628,13 +624,13 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
byte >>= 4;
else {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
byte = 0;
}
}
if (len & 1) {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
}
len = j;
goto grow;
@@ -659,13 +655,13 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
byte <<= 4;
else {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
byte = 0;
}
}
if (len & 1) {
char c = byte & 0xff;
- rb_str_buf_cat(res, &c, 1);
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
}
len = j;
goto grow;
@@ -681,7 +677,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
c = num2i32(from);
- rb_str_buf_cat(res, &c, sizeof(char));
+ CFDataAppendBytes(data, (const UInt8 *)&c, 1);
}
break;
@@ -692,7 +688,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
s = num2i32(from);
- rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
+ CFDataAppendBytes(data, (const UInt8 *)OFF16(&s), NATINT_LEN(short,2));
}
break;
@@ -703,7 +699,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
i = num2i32(from);
- rb_str_buf_cat(res, OFF32(&i), NATINT_LEN(int,4));
+ CFDataAppendBytes(data, (const UInt8 *)OFF32(&i), NATINT_LEN(int,4));
}
break;
@@ -714,7 +710,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
l = num2i32(from);
- rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
+ CFDataAppendBytes(data, (const UInt8 *)OFF32(&l), NATINT_LEN(long,4));
}
break;
@@ -725,7 +721,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
rb_quad_pack(tmp, from);
- rb_str_buf_cat(res, (char*)&tmp, QUAD_SIZE);
+ CFDataAppendBytes(data, (const UInt8 *)&tmp, QUAD_SIZE);
}
break;
@@ -736,7 +732,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
s = num2i32(from);
s = NATINT_HTONS(s);
- rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
+ CFDataAppendBytes(data, (const UInt8 *)OFF16(&s), NATINT_LEN(short,2));
}
break;
@@ -747,7 +743,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
l = num2i32(from);
l = NATINT_HTONL(l);
- rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
+ CFDataAppendBytes(data, (const UInt8 *)OFF32(&l), NATINT_LEN(long,4));
}
break;
@@ -758,7 +754,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
s = num2i32(from);
s = NATINT_HTOVS(s);
- rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
+ CFDataAppendBytes(data, (const UInt8 *)OFF16(&s), NATINT_LEN(short,2));
}
break;
@@ -769,7 +765,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
l = num2i32(from);
l = NATINT_HTOVL(l);
- rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
+ CFDataAppendBytes(data, (const UInt8 *)OFF32(&l), NATINT_LEN(long,4));
}
break;
@@ -780,7 +776,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
- rb_str_buf_cat(res, (char*)&f, sizeof(float));
+ CFDataAppendBytes(data, (const UInt8 *)&f, sizeof(float));
}
break;
@@ -792,7 +788,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
f = HTOVF(f,ftmp);
- rb_str_buf_cat(res, (char*)&f, sizeof(float));
+ CFDataAppendBytes(data, (const UInt8 *)&f, sizeof(float));
}
break;
@@ -804,7 +800,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
d = HTOVD(d,dtmp);
- rb_str_buf_cat(res, (char*)&d, sizeof(double));
+ CFDataAppendBytes(data, (const UInt8 *)&d, sizeof(double));
}
break;
@@ -815,7 +811,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
- rb_str_buf_cat(res, (char*)&d, sizeof(double));
+ CFDataAppendBytes(data, (const UInt8 *)&d, sizeof(double));
}
break;
@@ -827,7 +823,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
f = HTONF(f,ftmp);
- rb_str_buf_cat(res, (char*)&f, sizeof(float));
+ CFDataAppendBytes(data, (const UInt8 *)&f, sizeof(float));
}
break;
@@ -839,32 +835,37 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
d = HTOND(d,dtmp);
- rb_str_buf_cat(res, (char*)&d, sizeof(double));
+ CFDataAppendBytes(data, (const UInt8 *)&d, sizeof(double));
}
break;
case 'x': /* null byte */
grow:
while (len >= 10) {
- rb_str_buf_cat(res, nul10, 10);
+ CFDataAppendBytes(data, (const UInt8 *)nul10, 10);
len -= 10;
}
- rb_str_buf_cat(res, nul10, len);
+ CFDataAppendBytes(data, (const UInt8 *)nul10, len);
break;
case 'X': /* back up byte */
shrink:
- plen = RSTRING_LEN(res);
- if (plen < len)
+ plen = CFDataGetLength(data);
+ if (plen < len) {
rb_raise(rb_eArgError, "X outside of string");
- rb_str_set_len(res, plen - len);
+ }
+ CFDataSetLength(data, plen - len);
break;
case '@': /* null fill to absolute position */
- len -= RSTRING_LEN(res);
- if (len > 0) goto grow;
+ len -= CFDataGetLength(data);
+ if (len > 0) {
+ goto grow;
+ }
len = -len;
- if (len > 0) goto shrink;
+ if (len > 0) {
+ goto shrink;
+ }
break;
case '%':
@@ -884,7 +885,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
rb_raise(rb_eRangeError, "pack(U): value out of range");
}
le = rb_uv_to_utf8(buf, l);
- rb_str_buf_cat(res, (char*)buf, le);
+ CFDataAppendBytes(data, (const UInt8 *)buf, le);
}
break;
@@ -895,28 +896,33 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
ptr = RSTRING_PTR(from);
plen = RSTRING_LEN(from);
- if (len <= 2)
+ if (len <= 2) {
len = 45;
- else
+ }
+ else {
len = len / 3 * 3;
+ }
while (plen > 0) {
long todo;
- if (plen > len)
+ if (plen > len) {
todo = len;
- else
+ }
+ else {
todo = plen;
- encodes(res, ptr, todo, type);
+ }
+ encodes(data, ptr, todo, type);
plen -= todo;
ptr += todo;
}
break;
case 'M': /* quoted-printable encoded string */
from = rb_obj_as_string(NEXTFROM);
- if (len <= 1)
+ if (len <= 1) {
len = 72;
- qpencode(res, from, len);
+ }
+ qpencode(data, from, len);
break;
case 'P': /* pointer to packed byte string */
@@ -945,14 +951,14 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
}
rb_ary_push(associates, from);
rb_obj_taint(from);
- rb_str_buf_cat(res, (char*)&t, sizeof(char*));
+ CFDataAppendBytes(data, (const UInt8 *)&t, sizeof(char*));
}
break;
case 'w': /* BER compressed integer */
while (len-- > 0) {
unsigned long ul;
- VALUE buf = rb_str_new(0, 0);
+ CFMutableDataRef bufdata = CFDataCreateMutable(NULL, 0);
char c, *bufs, *bufe;
from = NEXTFROM;
@@ -961,40 +967,41 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
while (TYPE(from) == T_BIGNUM) {
from = rb_big_divmod(from, big128);
c = NUM2INT(RARRAY_AT(from, 1)) | 0x80; /* mod */
- rb_str_buf_cat(buf, &c, sizeof(char));
+ CFDataAppendBytes(bufdata, (const UInt8 *)&c, sizeof(char));
from = RARRAY_AT(from, 0); /* div */
}
}
- {
- long l = NUM2LONG(from);
- if (l < 0) {
- rb_raise(rb_eArgError, "can't compress negative numbers");
- }
- ul = l;
+ long l = NUM2LONG(from);
+ if (l < 0) {
+ rb_raise(rb_eArgError, "can't compress negative numbers");
}
+ ul = l;
while (ul) {
c = ((ul & 0x7f) | 0x80);
- rb_str_buf_cat(buf, &c, sizeof(char));
+ CFDataAppendBytes(bufdata, (const UInt8 *)&c, sizeof(char));
ul >>= 7;
}
- if (RSTRING_BYTELEN(buf)) {
- bufs = RSTRING_BYTEPTR(buf);
- bufe = bufs + RSTRING_BYTELEN(buf) - 1;
+ if (CFDataGetLength(bufdata) > 0) {
+ UInt8 *buf_beg = CFDataGetMutableBytePtr(bufdata);
+ bufs = (char *)buf_beg;
+ bufe = bufs + CFDataGetLength(bufdata) - 1;
*bufs &= 0x7f; /* clear continue bit */
while (bufs < bufe) { /* reverse */
c = *bufs;
*bufs++ = *bufe;
*bufe-- = c;
}
- rb_str_buf_cat(res, RSTRING_BYTEPTR(buf), RSTRING_BYTELEN(buf));
+ CFDataAppendBytes(data, buf_beg, CFDataGetLength(bufdata));
}
else {
c = 0;
- rb_str_buf_cat(res, &c, sizeof(char));
+ CFDataAppendBytes(data, (const UInt8 *)&c, sizeof(char));
}
+
+ CFRelease(bufdata);
}
break;
@@ -1004,9 +1011,10 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
}
if (associates) {
- rb_str_associate(res, associates);
+ rb_str_associate(bres, associates);
}
- return res;
+
+ return bres;
}
static const char uu_table[] =
@@ -1015,7 +1023,7 @@ static const char b64_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static void
-encodes(VALUE str, const char *s, long len, int type)
+encodes(CFMutableDataRef data, const char *s, long len, int type)
{
char *buff = ALLOCA_N(char, len * 4 / 3 + 6);
long i = 0;
@@ -1050,13 +1058,13 @@ encodes(VALUE str, const char *s, long len, int type)
buff[i++] = padding;
}
buff[i++] = '\n';
- rb_str_buf_cat(str, buff, i);
+ CFDataAppendBytes(data, (const UInt8 *)buff, i);
}
static const char hex_table[] = "0123456789ABCDEF";
static void
-qpencode(VALUE str, VALUE from, long len)
+qpencode(CFMutableDataRef data, VALUE from, long len)
{
char buff[1024];
long i = 0, n = 0, prev = EOF;
@@ -1094,7 +1102,7 @@ qpencode(VALUE str, VALUE from, long len)
prev = '\n';
}
if (i > 1024 - 5) {
- rb_str_buf_cat(str, buff, i);
+ CFDataAppendBytes(data, (const UInt8 *)buff, i);
i = 0;
}
s++;
@@ -1104,7 +1112,7 @@ qpencode(VALUE str, VALUE from, long len)
buff[i++] = '\n';
}
if (i > 0) {
- rb_str_buf_cat(str, buff, i);
+ CFDataAppendBytes(data, (const UInt8 *)buff, i);
}
}
@@ -1151,12 +1159,7 @@ hex2num(char c)
static VALUE
infected_str_new(const char *ptr, long len, VALUE str)
{
- VALUE s = rb_str_new(ptr, len);
-
-#if !WITH_OBJC
- OBJ_INFECT(s, str);
-#endif
- return s;
+ return rb_str_new(ptr, len);
}
/*
@@ -1424,14 +1427,19 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 8)
len = (send - s) * 8;
bits = 0;
- UNPACK_PUSH(bitstr = rb_str_new(0, len));
- t = RSTRING_BYTEPTR(bitstr);
+ bitstr = rb_bytestring_new();
+ rb_bytestring_resize(bitstr, len);
+ UNPACK_PUSH(bitstr);
+ t = (char *)rb_bytestring_byte_pointer(bitstr);
for (i=0; i<len; i++) {
- if (i & 7) bits >>= 1;
- else bits = *s++;
+ if (i & 7) {
+ bits >>= 1;
+ }
+ else {
+ bits = *s++;
+ }
*t++ = (bits & 1) ? '1' : '0';
}
- RSTRING_SYNC(bitstr);
}
break;
@@ -1445,14 +1453,19 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 8)
len = (send - s) * 8;
bits = 0;
- UNPACK_PUSH(bitstr = rb_str_new(0, len));
- t = RSTRING_BYTEPTR(bitstr);
+ bitstr = rb_bytestring_new();
+ rb_bytestring_resize(bitstr, len);
+ UNPACK_PUSH(bitstr);
+ t = (char *)rb_bytestring_byte_pointer(bitstr);
for (i=0; i<len; i++) {
- if (i & 7) bits <<= 1;
- else bits = *s++;
+ if (i & 7) {
+ bits <<= 1;
+ }
+ else {
+ bits = *s++;
+ }
*t++ = (bits & 128) ? '1' : '0';
}
- RSTRING_SYNC(bitstr);
}
break;
@@ -1466,16 +1479,19 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 2)
len = (send - s) * 2;
bits = 0;
- UNPACK_PUSH(bitstr = rb_str_new(0, len));
- t = RSTRING_BYTEPTR(bitstr);
+ bitstr = rb_bytestring_new();
+ rb_bytestring_resize(bitstr, len);
+ UNPACK_PUSH(bitstr);
+ t = (char *)rb_bytestring_byte_pointer(bitstr);
for (i=0; i<len; i++) {
- if (i & 1)
+ if (i & 1) {
bits >>= 4;
- else
+ }
+ else {
bits = *s++;
+ }
*t++ = hexdigits[bits & 15];
}
- RSTRING_SYNC(bitstr);
}
break;
@@ -1489,16 +1505,19 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
if (p[-1] == '*' || len > (send - s) * 2)
len = (send - s) * 2;
bits = 0;
- UNPACK_PUSH(bitstr = rb_str_new(0, len));
- t = RSTRING_BYTEPTR(bitstr);
+ bitstr = rb_bytestring_new();
+ rb_bytestring_resize(bitstr, len);
+ UNPACK_PUSH(bitstr);
+ t = (char *)rb_bytestring_byte_pointer(bitstr);
for (i=0; i<len; i++) {
- if (i & 1)
+ if (i & 1) {
bits <<= 4;
- else
+ }
+ else {
bits = *s++;
+ }
*t++ = hexdigits[(bits >> 4) & 15];
}
- RSTRING_SYNC(bitstr);
}
break;
@@ -1744,9 +1763,11 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
case 'u':
{
- VALUE buf = infected_str_new(0, (send - s)*3/4, str);
- char *ptr = RSTRING_BYTEPTR(buf);
+ VALUE buf = rb_bytestring_new();
+ rb_bytestring_resize(buf, (send - s)*3/4);
+ char *ptr = (char *)rb_bytestring_byte_pointer(buf);
long total = 0;
+ const long buflen = rb_bytestring_length(buf);
while (s < send && *s > ' ' && *s < 'a') {
long a,b,c,d;
@@ -1755,53 +1776,67 @@ pack_unpack(VALUE str, SEL sel, VALUE fmt)
hunk[3] = '\0';
len = (*s++ - ' ') & 077;
total += len;
- if (total > RSTRING_BYTELEN(buf)) {
- len -= total - RSTRING_BYTELEN(buf);
- total = RSTRING_BYTELEN(buf);
+ if (total > buflen) {
+ len -= total - buflen;
+ total = buflen;
}
while (len > 0) {
long mlen = len > 3 ? 3 : len;
- if (s < send && *s >= ' ')
+ if (s < send && *s >= ' ') {
a = (*s++ - ' ') & 077;
- else
+ }
+ else {
a = 0;
- if (s < send && *s >= ' ')
+ }
+ if (s < send && *s >= ' ') {
b = (*s++ - ' ') & 077;
- else
+ }
+ else {
b = 0;
- if (s < send && *s >= ' ')
+ }
+ if (s < send && *s >= ' ') {
c = (*s++ - ' ') & 077;
- else
+ }
+ else {
c = 0;
- if (s < send && *s >= ' ')
+ }
+ if (s < send && *s >= ' ') {
d = (*s++ - ' ') & 077;
- else
+ }
+ else {
d = 0;
+ }
hunk[0] = a << 2 | b >> 4;
hunk[1] = b << 4 | c >> 2;
hunk[2] = c << 6 | d;
memcpy(ptr, hunk, mlen);
ptr += mlen;
len -= mlen;
}
- if (*s == '\r') s++;
- if (*s == '\n') s++;
- else if (s < send && (s+1 == send || s[1] == '\n'))
+ if (*s == '\r') {
+ s++;
+ }
+ if (*s == '\n') {
+ s++;
+ }
+ else if (s < send && (s+1 == send || s[1] == '\n')) {
s += 2; /* possible checksum byte */
+ }
}
- rb_str_set_len(buf, total);
- RSTRING_SYNC(buf);
+ rb_bytestring_resize(buf, total);
UNPACK_PUSH(buf);
}
break;
case 'm':
{
- VALUE buf = infected_str_new(0, (send - s)*3/4, str);