📄 cpstringutils.cpp
字号:
/* ==================================================================== * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #include "CPStringUtils.hpp"#include "ErrorHandling.hpp"#define kNumberFormatString "\p########0.00#######;-########0.00#######"// Useful utility functions which could be optimized a whole lotvoid CopyPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength){int i,numPChars; if (thePStr != nil && theCStr != nil && maxCStrLength > 0) { numPChars = thePStr[0]; for (i = 0;;i++) { if (i >= numPChars || i >= maxCStrLength - 1) { theCStr[i] = 0; break; } else { theCStr[i] = thePStr[i + 1]; } } }}void CopyPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength){int theMaxDstStrLength; theMaxDstStrLength = maxDstStrLength; if (theDstPStr != nil && theSrcPStr != nil && theMaxDstStrLength > 0) { if (theMaxDstStrLength > 255) { theMaxDstStrLength = 255; } if (theMaxDstStrLength - 1 < theSrcPStr[0]) { BlockMove(theSrcPStr + 1,theDstPStr + 1,theMaxDstStrLength - 1); theDstPStr[0] = theMaxDstStrLength - 1; } else { BlockMove(theSrcPStr,theDstPStr,theSrcPStr[0] + 1); } }}void CopyCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxDstStrLength){int i; if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0) { for (i = 0;;i++) { if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1) { theDstCStr[i] = 0; break; } else { theDstCStr[i] = theSrcCStr[i]; } } }}void CopyCSubstrToCStr(const char *theSrcCStr,const int maxCharsToCopy,char *theDstCStr,const int maxDstStrLength){int i; if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0) { for (i = 0;;i++) { if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1 || i >= maxCharsToCopy) { theDstCStr[i] = 0; break; } else { theDstCStr[i] = theSrcCStr[i]; } } }}void CopyCSubstrToPStr(const char *theSrcCStr,const int maxCharsToCopy,unsigned char *theDstPStr,const int maxDstStrLength){int i;int theMaxDstStrLength; theMaxDstStrLength = maxDstStrLength; if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0) { if (theMaxDstStrLength > 255) { theMaxDstStrLength = 255; } for (i = 0;;i++) { if (theSrcCStr[i] == 0 || i >= theMaxDstStrLength - 1 || i >= maxCharsToCopy) { theDstPStr[0] = i; break; } else { theDstPStr[i + 1] = theSrcCStr[i]; } } }}void CopyCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength){int i;int theMaxDstStrLength; theMaxDstStrLength = maxDstStrLength; if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0) { if (theMaxDstStrLength > 255) { theMaxDstStrLength = 255; } for (i = 0;;i++) { if (i >= theMaxDstStrLength - 1 || theSrcCStr[i] == 0) { theDstPStr[0] = i; break; } else { theDstPStr[i + 1] = theSrcCStr[i]; } } }}void ConcatPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength){int i,numPChars,cStrLength; if (thePStr != nil && theCStr != nil && maxCStrLength > 0) { for (cStrLength = 0;theCStr[cStrLength] != 0;cStrLength++) { } numPChars = thePStr[0]; for (i = 0;;i++) { if (i >= numPChars || cStrLength >= maxCStrLength - 1) { theCStr[cStrLength++] = 0; break; } else { theCStr[cStrLength++] = thePStr[i + 1]; } } }}void ConcatPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength){int theMaxDstStrLength; theMaxDstStrLength = maxDstStrLength; if (theSrcPStr != nil && theDstPStr != nil && theMaxDstStrLength > 0) { if (theMaxDstStrLength > 255) { theMaxDstStrLength = 255; } if (theMaxDstStrLength - theDstPStr[0] - 1 < theSrcPStr[0]) { BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theMaxDstStrLength - 1 - theDstPStr[0]); theDstPStr[0] = theMaxDstStrLength - 1; } else { BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theSrcPStr[0]); theDstPStr[0] += theSrcPStr[0]; } }}void ConcatCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength){int i,thePStrLength;int theMaxDstStrLength; theMaxDstStrLength = maxDstStrLength; if (theSrcCStr != nil && theDstPStr != nil && theMaxDstStrLength > 0) { if (theMaxDstStrLength > 255) { theMaxDstStrLength = 255; } thePStrLength = theDstPStr[0]; for (i = 0;;i++) { if (theSrcCStr[i] == 0 || thePStrLength >= theMaxDstStrLength - 1) { theDstPStr[0] = thePStrLength; break; } else { theDstPStr[thePStrLength + 1] = theSrcCStr[i]; thePStrLength++; } } }}void ConcatCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxCStrLength){int cStrLength; if (theSrcCStr != nil && theDstCStr != nil && maxCStrLength > 0) { for (cStrLength = 0;theDstCStr[cStrLength] != 0;cStrLength++) { } for (;;) { if (*theSrcCStr == 0 || cStrLength >= maxCStrLength - 1) { theDstCStr[cStrLength++] = 0; break; } else { theDstCStr[cStrLength++] = *theSrcCStr++; } } }}void ConcatCharToCStr(const char theChar,char *theDstCStr,const int maxCStrLength){int cStrLength; if (theDstCStr != nil && maxCStrLength > 0) { cStrLength = CStrLength(theDstCStr); if (cStrLength < maxCStrLength - 1) { theDstCStr[cStrLength++] = theChar; theDstCStr[cStrLength++] = '\0'; } }}void ConcatCharToPStr(const char theChar,unsigned char *theDstPStr,const int maxPStrLength){int pStrLength; if (theDstPStr != nil && maxPStrLength > 0) { pStrLength = PStrLength(theDstPStr); if (pStrLength < maxPStrLength - 1 && pStrLength < 255) { theDstPStr[pStrLength + 1] = theChar; theDstPStr[0] += 1; } }}int CompareCStrs(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase){int returnValue;char firstChar,secondChar; returnValue = 0; if (theFirstCStr != nil && theSecondCStr != nil) { for (;;) { firstChar = *theFirstCStr; secondChar = *theSecondCStr; if (ignoreCase == true) { if (firstChar >= 'A' && firstChar <= 'Z') { firstChar = 'a' + (firstChar - 'A'); } if (secondChar >= 'A' && secondChar <= 'Z') { secondChar = 'a' + (secondChar - 'A'); } } if (firstChar == 0 && secondChar != 0) { returnValue = -1; break; } else if (firstChar != 0 && secondChar == 0) { returnValue = 1; break; } else if (firstChar == 0 && secondChar == 0) { returnValue = 0; break; } else if (firstChar < secondChar) { returnValue = -1; break; } else if (firstChar > secondChar) { returnValue = 1; break; } theFirstCStr++; theSecondCStr++; } } return(returnValue);}Boolean CStrsAreEqual(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase){ if (CompareCStrs(theFirstCStr,theSecondCStr,ignoreCase) == 0) { return true; } else { return false; }}Boolean PStrsAreEqual(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase){ if (ComparePStrs(theFirstPStr,theSecondPStr,ignoreCase) == 0) { return true; } else { return false; }}int ComparePStrs(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase){int i,returnValue;char firstChar,secondChar; returnValue = 0; if (theFirstPStr != nil && theSecondPStr != nil) { for (i = 1;;i++) { firstChar = theFirstPStr[i]; secondChar = theSecondPStr[i]; if (ignoreCase == true) { if (firstChar >= 'A' && firstChar <= 'Z') { firstChar = 'a' + (firstChar - 'A'); } if (secondChar >= 'A' && secondChar <= 'Z') { secondChar = 'a' + (secondChar - 'A'); } } if (theFirstPStr[0] < i && theSecondPStr[0] >= i) { returnValue = -1; break; } else if (theFirstPStr[0] >= i && theSecondPStr[0] < i) { returnValue = 1; break; } else if (theFirstPStr[0] < i && theSecondPStr[0] < i) { returnValue = 0; break; } else if (firstChar < secondChar) { returnValue = -1; break; } else if (firstChar > secondChar) { returnValue = 1; break; } } } return(returnValue);}int CompareCStrToPStr(const char *theCStr,const unsigned char *thePStr,const Boolean ignoreCase){int returnValue;char tempString[256]; returnValue = 0; if (theCStr != nil && thePStr != nil) { CopyPStrToCStr(thePStr,tempString,sizeof(tempString)); returnValue = CompareCStrs(theCStr,tempString,ignoreCase); } return(returnValue);}void ConcatLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits){Str255 theStr255; NumToString(theNum,theStr255); if (numDigits > 0) { int charsToInsert; charsToInsert = numDigits - PStrLength(theStr255); if (charsToInsert > 0) { char tempString[256]; CopyCStrToCStr("",tempString,sizeof(tempString)); for (;charsToInsert > 0;charsToInsert--) { ConcatCStrToCStr("0",tempString,sizeof(tempString)); } ConcatPStrToCStr(theStr255,tempString,sizeof(tempString)); CopyCStrToPStr(tempString,theStr255,sizeof(theStr255)); } } ConcatPStrToCStr(theStr255,theCStr,maxCStrLength);}void ConcatLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits){Str255 theStr255; NumToString(theNum,theStr255); if (numDigits > 0) { int charsToInsert; charsToInsert = numDigits - PStrLength(theStr255); if (charsToInsert > 0) { char tempString[256]; CopyCStrToCStr("",tempString,sizeof(tempString)); for (;charsToInsert > 0;charsToInsert--) { ConcatCStrToCStr("0",tempString,sizeof(tempString)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -