/* librock/text/count.c: librock_CHISEL _summary string and character counting variants Table of Contents. [License and Disclaimer of Warranty] [License Attributes] [How to Reuse this software] [Definitions and header file inclusions] [Function Implementation and inline documentation] [Revision summary and Changelog] */ /*************************************************************/ /***[License and Disclaimer of Warranty]**********************/ /* By copyright law, you cannot modify or copy this software or derivative works without accepting the license. Copyright (c) 2004, Forrest J. Cavalier III Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************/ /***[License Attributes]**************************************/ /* The librock LIDESC tool reports some license attributes. It reports the following for the MIT license used for this software: A notice must be included in documentation or other materials when a binary is distributed. (librock_DOCNOTE_1C_WITHBIN) Notices must remain in source code. (librock_SRCNOTE_1) The Free Software Foundation says this license is a Free software license. (librock_LIDESC_COMBINE_FSF_OK) The Free Software Foundation says this license is GPL compatible. (librock_LIDESC_COMBINE_GPL_OK) The Open Source Initiative approved this license as OSD compliant. (librock_LIDESC_COMBINE_OSD_OK) Note: You may be able to use LIDESC tools to check compatibility with other licenses. */ /*************************************************************/ /***[How to Reuse this software]******************************/ /* 1. Agree to the license and disclaimer of warranty. 2. Read this file for function documentation and examples. Verify the implementation is suitable for you. 3. Define preprocessor symbols for your machine and compilation environment. This file is originally from libROCK: the Reusable Opensource Code and Knowledge library. For an automated update and build system, license certificates, and more, see http://www.mibsoftware.com/librock/ */ /*************************************************************/ /***[Definitions and header file inclusions]******************/ #ifdef librock_ISOLATED #else #define librock_IMPLEMENT_count #include #include #include #ifndef librock_assert #define librock_assert(a) #endif #endif /* ifdef librock_ISOLATED else */ /*************************************************************/ /***[Function Implementation and inline documentation]********/ #ifdef librock_IMPL_LIDESC /**************************************************************/ #include /* librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ void *librock_LIDESC_count[] = { "\n" __FILE__ librock_LIDESC_mit "\n", 0 }; /**************************************************************/ #endif /**************************************************************/ int librock_counttowh(librock_CONST char librock_PTR *pStart) { /* Count consecutive characters > ' ' at pStart */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_counttowh /* librock_counttowh - count consecutive non-blank characters */ /**/ #include int librock_counttowh( const char *psz /* NUL terminated string */ ); /* Returns count of consecutive non-blank characters at psz (psz[i] > ' ')||(psz[i] & 0x80) Typical use is to advance a pointer, replacing code like: */ while(*ptr > ' ') ptr++; /* with: */ ptr += librock_counttowh(ptr); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_counttowh(ptr),stdout); /* librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST unsigned char librock_PTR *ptr; /* Made unsigned 10/1/96 */ ptr = (librock_CONST unsigned char librock_PTR *) pStart; while((*ptr & 0x80)||(*ptr > ' ')) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return (int) ((librock_CONST char librock_PTR *) ptr - pStart); } /* librock_counttowh */ /**************************************************************/ /**************************************************************/ int librock_countbl(librock_CONST char librock_PTR *pStart) { /* Count consecutive characters ==' ' or '\t' */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_countbl /* librock_countbl - counts consecutive blank characters (ASCII TAB or ' ') */ /**/ #include int librock_countbl( const char *psz /* NUL terminated string */ ); /* Returns count of consecutive blank characters (ASCII TAB or ' ') Typical use is to advance a pointer, replacing code like: */ while((*ptr == ' ')||(*ptr == '\t')) ptr++; /* with: */ ptr += librock_countbl(ptr); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_countbl(ptr),stdout); /* librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST char librock_PTR *ptr; ptr = pStart; while((*ptr == ' ')||(*ptr == '\t')) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return (int) (ptr-pStart); } /* librock_countbl */ /**************************************************************/ /**************************************************************/ int librock_countln(librock_CONST char librock_PTR *pStart) { /* count characters up to and including next '\n' or '\0' */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_countln /* librock_countln - count consecutive characters up to and including next \0 \n */ /**/ #include int librock_countln( const char *psz ); /* Returns count of consecutive characters up to and including next \0 \n IMPORTANT: this will go beyond \0 in a string. See example "Iterating Lines" Typical use is to advance a pointer, replacing code like: */ while((*ptr != '\r')&&(*ptr != '\n')&&(*ptr != '\0')) ptr++; ptr++; /* with: */ ptr += librock_countln(ptr); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_countln(ptr),stdout); /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ /* librock_assert() // #ifdef librock_DEBUG */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST char librock_PTR *ptr; ptr = pStart; while(*ptr &&(*ptr != '\n')) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return (int) (ptr-pStart+1); } /* librock_countln */ /**************************************************************/ /**************************************************************/ int librock_counttoeol(librock_CONST char librock_PTR *pStart) { /* count characters up to next up to next '\n' '\r' or '\0' */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_counttoeol /* librock_counttoeol - count consecutive characters up to (but not including) next \0 \n or \r */ /**/ #include int librock_counttoeol( const char *psz ); /* Returns count of consecutive characters up to (but not including) next \0 \n or \r Typical use is to advance a pointer, replacing code like: */ while((*ptr != '\r')&&(*ptr != '\n')&&(*ptr != '\0')) ptr++; /* with: */ ptr += librock_counttoeol(ptr); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_counttoeol(ptr),stdout); /* Also used to chop off the newline part of a string buffer. For example: */ buffer[librock_counttoeol(buffer)] = '\0'; /* librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST char librock_PTR *ptr; ptr = pStart; while(*ptr && (*ptr != '\n')&&(*ptr != '\r')) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return ptr-pStart; } /**************************************************************/ /**************************************************************/ int librock_counttoch(librock_CONST char librock_PTR *pStart,char ch) { /* returns number of characters up to next ch or '\0' */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_counttoch /* librock_counttoch - count consecutive characters up to but not including next ch or \0 */ /**/ #include int librock_counttoch( const char *psz, char ch ); /* Returns count of consecutive characters up to but not including next ch or \0 Typical use is to advance a pointer, replacing code like: */ while((*ptr != ch)&&(*ptr != '\0')) ptr++; /* with: */ ptr += librock_counttoch(ptr,ch); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_counttoch(ptr,'\t'),stdout); /* librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST char librock_PTR *ptr = pStart; while(*ptr && (*ptr != ch)) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return ptr - pStart; } /* librock_counttoch */ /**************************************************************/ /**************************************************************/ int librock_counttochn(const char *pStart,char ch,int n) { /* returns number of characters up to n'th ch or '\0' */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_counttochn /* librock_counttochn - count consecutive characters up to but not including n'th ch or \0 */ /**/ #include int librock_counttochn( const char *psz, char ch int n); /* Returns count of consecutive characters up to but not including n'th ch or \0 Typical use is to advance a pointer to the n'th field of a string with fields delimited by a fixed character. */ ptr += librock_counttochn(ptr,'\t',2); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_counttoch(ptr,'\t',2),stdout); /* librock_counttoch() librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ const char *ptr; if (n <= 0) { return 0; } ptr = pStart; while ((n > 0)&&(*ptr)) { ptr += librock_counttoch(ptr,ch); n--; if (n == 0) { break; } if (*ptr) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } } return ptr-pStart; } /* librock_counttochn */ /**************************************************************/ /**************************************************************/ int librock_countCid(librock_CONST char librock_PTR *pStart) { /* returns number of characters comprising a standard C identifier. */ /*Copyright 1998-2004 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_countCid /* librock_countCid - count consecutive characters in a C language identifier ([A-Za-z_][A-Za-z0-9_]*) */ /**/ #include int librock_countCid( const char *psz ); /* Returns count of consecutive characters in a C language identifier First letter in [A-Za-z_] Rest: [A-Za-z0-9_]* Typical use is to advance a pointer */ ptr += librock_countCid(ptr); /* Also used in parsing, where the count is needed to copy, compare, or write a limited number of characters starting at ptr. For example: */ fwrite(ptr,1,librock_countCid(ptr),stdout); /* librock_assert() // #ifdef librock_DEBUG */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ librock_CONST char librock_PTR *ptr = pStart; if ((*pStart != '_') && !( ((*pStart <= 'Z')&&(*pStart >= 'A'))|| ((*pStart <= 'z')&&(*pStart >= 'a'))) ) { return 0; } while( (*ptr == '_')|| ((*ptr <= 'Z')&&(*ptr >= 'A'))|| ((*ptr <= 'z')&&(*ptr >= 'a'))|| ((*ptr <= '9')&&(*ptr >= '0'))) { ptr++; #ifdef librock_DEBUG if ((librock_CONST char librock_PTR *) ptr <= pStart) { librock_assert(0&&"debugging check in librock. pointer overflow"); } #endif } return ptr - pStart; } /* librock_countCid */ /**************************************************************/ /***[Revision summary and Changelog]**************************/ /* $Log: count.c,v $ Revision 1.7 2002/08/01 14:14:45 fcavalier@mibsoftware.com rights=#1 Moved CVS log to end. Added NOTLIBROCK section. Changed LIDESC MD5 to HC. Revision 1.6 2002/04/19 14:51:49 fcavalier@mibsoftware.com rights=#1 Manual page fixes. Revision 1.5 2002/04/09 03:28:48 fcavalier@mibsoftware.com rights=#1 FNTYPEs added. Updated LICENSE in manual. Revision 1.4 2002/04/06 04:00:34 fcavalier@mibsoftware.com rights=#1 Add FNTYPE decls for regression tests. Revision 1.3 2002/02/05 21:17:01 fcavalier@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:08:22 fcavalier@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:25:14 fcavalier@mibsoftware.com rights=#1 moved rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */