📄 slstrops.c
字号:
/* end of string and end not found. Just truncate it a return; */ *mark = 0; break; } strcpy ((char *) mark, (char *)s); s = mark; } SLang_push_malloced_string (str);}/*}}}*/static void str_quote_string_cmd (char *str, char *quotes, int *slash_ptr) /*{{{*/{ char *q; int slash; unsigned int len; char *s, *q1; unsigned char ch; unsigned char *t; slash = *slash_ptr; if ((slash > 255) || (slash < 0)) { SLang_Error = SL_INVALID_PARM; return; } /* setup the utility table to have 1s at quote char postitions. */ set_utility_char_table (quotes); t = Utility_Char_Table; t[(unsigned int) slash] = 1; /* calculate length */ s = str; len = 0; while ((ch = (unsigned char) *s++) != 0) if (t[ch]) len++; len += (unsigned int) (s - str); if (NULL != (q = SLmalloc(len))) { s = str; q1 = q; while ((ch = (unsigned char) *s++) != 0) { if (t[ch]) *q1++ = slash; *q1++ = (char) ch; } *q1 = 0; SLang_push_malloced_string(q); }}/*}}}*//* returns the position of substrin in a string or null */static int issubstr_cmd (char *a, char *b) /*{{{*/{ char *c; if (NULL == (c = (char *) strstr(a, b))) return 0; return 1 + (int) (c - a);}/*}}}*//* returns to stack string at pos n to n + m of a */static void substr_cmd (char *a, int *n_ptr, int *m_ptr) /*{{{*/{ int n, m; int lena; n = *n_ptr; m = *m_ptr; lena = strlen (a); if (n > lena) n = lena + 1; if (n < 1) { SLang_Error = SL_INVALID_PARM; return; } n--; if (m < 0) m = lena; if (n + m > lena) m = lena - n; (void) _SLang_push_nstring (a + n, (unsigned int) m);}/*}}}*//* substitute char m at positin string n in string*/static void strsub_cmd (int *nptr, int *mptr) /*{{{*/{ char *a; int n, m; unsigned int lena; if (-1 == SLpop_string (&a)) return; n = *nptr; m = *mptr; lena = strlen (a); if ((n <= 0) || (lena < (unsigned int) n)) { SLang_Error = SL_INVALID_PARM; SLfree(a); return; } a[n - 1] = (char) m; SLang_push_malloced_string (a);}/*}}}*/static void strup_cmd(void) /*{{{*/{ unsigned char c, *a; char *str; if (SLpop_string (&str)) return; a = (unsigned char *) str; while ((c = *a) != 0) { /* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */ *a = UPPER_CASE(c); a++; } SLang_push_malloced_string (str);}/*}}}*/static int isdigit_cmd (char *what) /*{{{*/{ return isdigit((unsigned char)*what);}/*}}}*/static int toupper_cmd (int *ch) /*{{{*/{ return UPPER_CASE(*ch);}/*}}}*/static int tolower_cmd (int *ch) /*{{{*/{ return LOWER_CASE(*ch);}/*}}}*/static void strlow_cmd (void) /*{{{*/{ unsigned char c, *a; char *str; if (SLpop_string(&str)) return; a = (unsigned char *) str; while ((c = *a) != 0) { /* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */ *a = LOWER_CASE(c); a++; } SLang_push_malloced_string ((char *) str);}/*}}}*/static SLang_Array_Type *do_strchop (char *str, int delim, int quote){ int count; char *s0, *elm; register char *s1; register unsigned char ch; int quoted; SLang_Array_Type *at; char **data; if ((quote < 0) || (quote > 255) || (delim <= 0) || (delim > 255)) { SLang_Error = SL_INVALID_PARM; return NULL; } s1 = s0 = str; quoted = 0; count = 1; /* at least 1 */ while (1) { ch = (unsigned char) *s1++; if ((ch == quote) && quote) { if (*s1 == 0) break; s1++; continue; } if (ch == delim) { count++; continue; } if (ch == 0) break; } if (NULL == (at = SLang_create_array (SLANG_STRING_TYPE, 0, NULL, &count, 1))) return NULL; data = (char **)at->data; count = 0; s1 = s0; while (1) { ch = (unsigned char) *s1; if ((ch == quote) && quote) { s1++; if (*s1 != 0) s1++; quoted = 1; continue; } if ((ch == delim) || (ch == 0)) { if (quoted == 0) elm = SLang_create_nslstring (s0, (unsigned int) (s1 - s0)); else { register char ch1, *p, *p1; char *tmp; tmp = SLmake_nstring (s0, (unsigned int)(s1 - s0)); if (tmp == NULL) break; /* Now unquote it */ p = p1 = tmp; do { ch1 = *p1++; if (ch1 == '\\') ch1 = *p1++; *p++ = ch1; } while (ch1 != 0); quoted = 0; elm = SLang_create_slstring (tmp); SLfree (tmp); } if (elm == NULL) break; data[count] = elm; count++; if (ch == 0) return at; s1++; /* skip past delim */ s0 = s1; /* and reset */ } else s1++; } SLang_free_array (at); return NULL;}static void strchop_cmd (char *str, int *q, int *d){ (void) SLang_push_array (do_strchop (str, *q, *d), 1);}static void strchopr_cmd (char *str, int *q, int *d){ SLang_Array_Type *at; if (NULL != (at = do_strchop (str, *q, *d))) { char **d0, **d1; d0 = (char **) at->data; d1 = d0 + (at->num_elements - 1); while (d0 < d1) { char *tmp; tmp = *d0; *d0 = *d1; *d1 = tmp; d0++; d1--; } } SLang_push_array (at, 1);}static int strcmp_cmd (char *a, char *b) /*{{{*/{ return strcmp(a, b);}/*}}}*/static int strncmp_cmd (char *a, char *b, int *n) /*{{{*/{ return strncmp(a, b, (unsigned int) *n);}/*}}}*/static int strlen_cmd (char *s) /*{{{*/{ return (int) strlen (s);}/*}}}*/static void extract_element_cmd (char *list, int *nth_ptr, int *delim_ptr){ char buf[1024], *b; b = buf; if (-1 == SLextract_list_element (list, *nth_ptr, *delim_ptr, buf, sizeof(buf))) b = NULL; SLang_push_string (b);}/* sprintf functionality for S-Lang */static char *SLdo_sprintf (char *fmt) /*{{{*/{ register char *p = fmt, ch; char *out = NULL, *outp = NULL; char dfmt[1024]; /* used to hold part of format */ char *f; VOID_STAR varp; int want_width, width, precis, use_varp, int_var; long long_var; unsigned int len = 0, malloc_len = 0, dlen; int do_free, guess_size;#if SLANG_HAS_FLOAT int tmp1, tmp2, use_double; double x;#endif int use_long = 0; while (1) { while ((ch = *p) != 0) { if (ch == '%') break; p++; } /* p points at '%' or 0 */ dlen = (unsigned int) (p - fmt); if (len + dlen >= malloc_len) { malloc_len = len + dlen; if (out == NULL) outp = SLmalloc(malloc_len + 1); else outp = SLrealloc(out, malloc_len + 1); if (NULL == outp) return out; out = outp; outp = out + len; } strncpy(outp, fmt, dlen); len += dlen; outp = out + len; *outp = 0; if (ch == 0) break; /* bump it beyond '%' */ ++p; fmt = p; f = dfmt; *f++ = ch; /* handle flag char */ ch = *p++; /* Make sure cases such as "% #g" can be handled. */ if ((ch == '-') || (ch == '+') || (ch == ' ') || (ch == '#')) { *f++ = ch; ch = *p++; if ((ch == '-') || (ch == '+') || (ch == ' ') || (ch == '#')) { *f++ = ch; ch = *p++; } } /* width */ /* I have got to parse it myself so that I can see how big it needs * to be. */ want_width = width = 0; if (ch == '*') { if (SLang_pop_integer(&width)) return (out); want_width = 1; ch = *p++; } else { if (ch == '0') { *f++ = '0'; ch = *p++; } while ((ch <= '9') && (ch >= '0')) { width = width * 10 + (ch - '0'); ch = *p++; want_width = 1; } } if (want_width) { sprintf(f, "%d", width); f += strlen (f); } precis = 0; /* precision -- also indicates max number of chars from string */ if (ch == '.') { *f++ = ch; ch = *p++; want_width = 0; if (ch == '*') { if (SLang_pop_integer(&precis)) return (out); ch = *p++; want_width = 1; } else while ((ch <= '9') && (ch >= '0')) { precis = precis * 10 + (ch - '0'); ch = *p++; want_width = 1; } if (want_width) { sprintf(f, "%d", precis); f += strlen (f); } else precis = 0; } long_var = 0; int_var = 0; varp = NULL; guess_size = 32;#if SLANG_HAS_FLOAT use_double = 0;#endif use_long = 0; use_varp = 0; do_free = 0; if (ch == 'l') { use_long = 1; ch = *p++; } else if (ch == 'h') ch = *p++; /* not supported */ /* Now the actual format specifier */ switch (ch) { case 'S': _SLstring_intrinsic (); ch = 's'; /* drop */ case 's': if (SLang_pop_slstring((char **) &varp)) return (out); do_free = 1; guess_size = strlen((char *) varp); use_varp = 1; break; case '%': guess_size = 1; do_free = 0; use_varp = 1; varp = (VOID_STAR) "%"; break; case 'c': guess_size = 1; use_long = 0; /* drop */ case 'd': case 'i': case 'o': case 'u': case 'X': case 'x': if (SLang_pop_long (&long_var)) return(out); if (use_long == 0) int_var = (int) long_var; else *f++ = 'l'; break; case 'f': case 'e': case 'g': case 'E': case 'G':#if SLANG_HAS_FLOAT if (SLang_pop_double(&x, &tmp1, &tmp2)) return (out); use_double = 1; guess_size = 256; (void) tmp1; (void) tmp2; use_long = 0; break;#endif case 'p': guess_size = 32; /* Pointer type?? Why?? */ if (-1 == SLdo_pop ()) return out; varp = (VOID_STAR) _SLang_get_run_stack_pointer (); use_varp = 1; use_long = 0; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -