OSDN Git Service

Added extra helper methods to BYTE_VECTOR
authorJohan t Hart <johanthart@gmail.com>
Mon, 2 Feb 2009 21:22:53 +0000 (22:22 +0100)
committerFrank Li <lznuaa@gmail.com>
Sun, 8 Feb 2009 07:04:46 +0000 (15:04 +0800)
src/Git/gittype.h

index a8b3028..2e6474d 100644 (file)
@@ -38,6 +38,29 @@ public:
 \r
                return pos;\r
        }\r
+       int findData(const BYTE* dataToFind, size_t dataSize, int start=0)\r
+       {\r
+               const BYTE* pos=&*begin()+start;\r
+               const BYTE* dataEnd=&*end()-(dataSize-1);//Set end one step after last place to search\r
+               if(pos>=dataEnd)\r
+                       return -1;//Started over end. Return not found\r
+               if(dataSize==0)\r
+                       return start;//No search data. Return current position\r
+               BYTE firstByte=dataToFind[0];\r
+               while(pos<dataEnd)\r
+               {\r
+                       //memchr for first character\r
+                       const BYTE* found=(const BYTE*)memchr(pos,firstByte,dataEnd-pos);\r
+                       if(found==NULL)\r
+                               return -1;//Not found\r
+                       //check rest of characters\r
+                       if(memcmp(found,dataToFind,dataSize)==0)\r
+                               return found-&*begin();//Match. Return position.\r
+                       //No match. Set position on next byte and continue search\r
+                       pos=found+1;\r
+               }\r
+               return -1;\r
+       }\r
        int append( std::vector<BYTE> &v,int start=0,int end=-1)\r
        {\r
                if(end<0)\r
@@ -46,6 +69,13 @@ public:
                        this->push_back(v[i]);\r
                return 0;\r
        }\r
+       int append(const BYTE* data, size_t dataSize)\r
+       {\r
+               size_t oldsize=size();\r
+               resize(oldsize+dataSize);\r
+               memcpy(&*(begin()+oldsize),data,dataSize);\r
+               return 0;\r
+       }\r
 };\r
 typedef std::vector<CString> STRING_VECTOR;\r
 typedef std::map<CString, STRING_VECTOR> MAP_HASH_NAME;\r