golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/shootout/reverse-complement.c (about) 1 // +build ignore 2 3 /* 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are met: 6 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 10 * Redistributions in binary form must reproduce the above copyright 11 notice, this list of conditions and the following disclaimer in the 12 documentation and/or other materials provided with the distribution. 13 14 * Neither the name of "The Computer Language Benchmarks Game" nor the 15 name of "The Computer Language Shootout Benchmarks" nor the names of 16 its contributors may be used to endorse or promote products derived 17 from this software without specific prior written permission. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * The Computer Language Benchmarks Game 34 * http://shootout.alioth.debian.org 35 * 36 * contributed by Bob W 37 */ 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 42 #define JBFSIZE 82 // line input buffer size 43 #define QBFSIZE 5200 // output buffer initial size 44 #define Z16 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 45 #define V32 "\0TVGH\0\0CD\0\0M\0KN\0\0\0YSA\0BW\0R\0\0\0\0\0\0" 46 #define VALL Z16 Z16 Z16 Z16 V32 V32 Z16 Z16 Z16 Z16 Z16 Z16 Z16 Z16 47 48 int errex(char *s, int n) { // error message+value, return 1 49 fprintf(stderr,"\n*** Error: %s [%d]!\n", s, n); 50 return 1; 51 } 52 53 int main () { // ***** main ***** 54 char *pj, *pq, *pr; // buffer pointers: inp,out,/out 55 char *jjj = malloc(JBFSIZE); // allocate input line buffer 56 char *qqq = malloc(QBFSIZE); // output buffer (dyn. size) 57 char *pqstop = qqq+QBFSIZE; // end-of-buffer pointer 58 char xtab[256] = VALL; // char conversion table 59 60 if (!jjj || !qqq) 61 return errex("Buffer allocation", !jjj + !qqq); 62 pj = fgets(jjj,JBFSIZE,stdin); // fetch 1st line 63 if (!pj) 64 return errex("No input data",0); 65 if (*jjj != '>') 66 return errex("1st char not '>'", 0); 67 68 while (pj) { // MAIN LOOP: process data 69 fputs(jjj, stdout); // output ID line 70 71 for (pq=qqq+1, pr=pqstop; ; pq++) { // LOOP: fill output buffer 72 pj = fgets(jjj, JBFSIZE, stdin); // get line from stdin 73 if (!pj || (*jjj=='>')) break; // EOF or new ID line 74 if (pr <= (pq+61)) { // need to resize buffer 75 char *newstop = pqstop + 12777888; 76 char *newptr = realloc(qqq, newstop-qqq); 77 if (!newptr) 78 return errex("Out of memory", 0); 79 if (newptr != qqq) { // new base: adj. pointers 80 size_t x = newptr-qqq; // offset for pointer update 81 pq+=x; pr+=x; qqq+=x; 82 newstop+=x; pqstop+=x; 83 } 84 pr = __builtin_memmove(newstop-(pqstop-pr), pr, pqstop-pr); 85 pqstop = newstop; // buffer resize complete 86 } 87 while (*pj) { // LOOP: conv. & revert line 88 char c = xtab[(unsigned char)(*pj++)]; 89 if (c) // conversion valid 90 *(--pr) = c; 91 } 92 } 93 94 for (pq = qqq; pr<pqstop; ) { // LOOP: format output 95 size_t x = (pqstop-pr)<60 ? pqstop-pr : 60; 96 __builtin_memmove(pq,pr,x); // move line to free space 97 pr+=x; pq+=x; *(pq++) = 0xA; // adjust pointers, add LF 98 } 99 fwrite(qqq, 1, pq-qqq, stdout); // output converted data 100 } 101 return 0; 102 }