| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 /* | |
| 8 * str_utils.h | |
| 9 * | |
| 10 * Defines support string routines for the instruction enumerator. | |
| 11 */ | |
| 12 #ifndef NACL_TRUSTED_BUT_NOT_TCB | |
| 13 #error("This file is not meant for use in the TCB.") | |
| 14 #endif | |
| 15 | |
| 16 #include "native_client/src/trusted/validator/x86/testing/enuminsts/str_utils.h" | |
| 17 | |
| 18 #include <ctype.h> | |
| 19 #include <string.h> | |
| 20 | |
| 21 #include "native_client/src/trusted/validator/x86/testing/enuminsts/enuminsts.h" | |
| 22 | |
| 23 /* If string s begins with string prefix, return a pointer to the | |
| 24 * first byte after the prefix. Else return s. | |
| 25 */ | |
| 26 char *SkipPrefix(char *s, const char *prefix) { | |
| 27 size_t plen = strlen(prefix); | |
| 28 if (strncmp(s, prefix, plen) == 0) { | |
| 29 return &s[plen + 1]; | |
| 30 } | |
| 31 return s; | |
| 32 } | |
| 33 | |
| 34 /* Return a pointer to s with leading spaces removed. | |
| 35 */ | |
| 36 const char *strip(const char *s) { | |
| 37 while (*s == ' ') s++; | |
| 38 return s; | |
| 39 } | |
| 40 | |
| 41 /* Updates the string by removing trailing spaces/newlines. */ | |
| 42 void rstrip(char *s) { | |
| 43 size_t slen = strlen(s); | |
| 44 while (slen > 0) { | |
| 45 --slen; | |
| 46 if (!isspace(s[slen])) return; | |
| 47 s[slen] = '\0'; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 /* Find substring ss in string s. Returns a pointer to the substring | |
| 52 * on success, NULL on failure. | |
| 53 */ | |
| 54 const char *strfind(const char *s, const char *ss) { | |
| 55 size_t i; | |
| 56 size_t slen = strlen(s); | |
| 57 size_t sslen = strlen(ss); | |
| 58 | |
| 59 for (i = 0; i < slen; i++) { | |
| 60 if (s[i] == ss[0]) { | |
| 61 if (strncmp(&s[i], ss, sslen) == 0) { | |
| 62 return &s[i]; | |
| 63 } | |
| 64 } | |
| 65 } | |
| 66 return NULL; | |
| 67 } | |
| 68 | |
| 69 /* If string ss appears in string s, return a pointer to the first byte | |
| 70 * after ss. Otherwise return NULL. | |
| 71 */ | |
| 72 const char *strskip(const char *s, const char *ss) { | |
| 73 const char *tmp = strfind(s, ss); | |
| 74 if (tmp != NULL) { | |
| 75 return &tmp[strlen(ss)]; | |
| 76 } | |
| 77 return NULL; | |
| 78 } | |
| 79 | |
| 80 /* Remove all instances of character c in string s. */ | |
| 81 void strnzapchar(char *s, const char c) { | |
| 82 size_t i, nskip; | |
| 83 const size_t slen = strlen(s); | |
| 84 | |
| 85 if (0 == c) return; | |
| 86 nskip = 0; | |
| 87 for (i = 0; i + nskip <= slen; i++) { | |
| 88 while (s[i + nskip] == c) nskip += 1; | |
| 89 s[i] = s[i + nskip]; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 /* Remove all instances of substring ss in string s, modifying s in place. | |
| 94 */ | |
| 95 void CleanString(char *s, const char *ss) { | |
| 96 size_t sslen = strlen(ss); | |
| 97 char *fs = (char*) strfind(s, ss); | |
| 98 | |
| 99 while (fs != NULL) { | |
| 100 do { | |
| 101 fs[0] = fs[sslen]; | |
| 102 fs++; | |
| 103 } while (*fs != '\0'); | |
| 104 fs = (char*) strfind(s, ss); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 /* Copies source to dest, like strncpy, but replaces the last | |
| 109 * byte of the dest (based on buffer size) with a null character, | |
| 110 * so that we are guaranteed to have a valid string. | |
| 111 */ | |
| 112 void cstrncpy(char *dest, const char *src, size_t n) { | |
| 113 strncpy(dest, src, n); | |
| 114 dest[n-1] = '0'; | |
| 115 } | |
| 116 | |
| 117 /* Copy src to dest, stoping at character c. | |
| 118 */ | |
| 119 void strncpyto(char *dest, const char *src, size_t n, char c) { | |
| 120 size_t i; | |
| 121 | |
| 122 i = 0; | |
| 123 while (1) { | |
| 124 dest[i] = src[i]; | |
| 125 if (dest[i] == c) { | |
| 126 dest[i] = 0; | |
| 127 break; | |
| 128 } | |
| 129 if (dest[i] == '\0') break; | |
| 130 i++; | |
| 131 if (i == n) InternalError("really long opcode"); | |
| 132 } | |
| 133 } | |
| OLD | NEW |