char* MiniApache::GetGETVariable(char * name){ char * varp = JoinStr(name, "="); int dlen = strlen(GET_data); int vlen = strlen(varp); int start = -1; char * sub; for (int i = 0; i < dlen; i++) { // Find variable sub = SubStr(GET_data, i, i + vlen); if (strcmp(varp, sub) == 0 && (i == 0 || GET_data[i-1] == '&')) { start = i + vlen; delete [] sub; break; } delete [] sub; } if (start == -1) { start = dlen; } int end = FindChar(GET_data, '&', start, dlen); if (end == -1) { end = dlen; } delete [] varp; char * retVal = SubStr(GET_data, start, end); return retVal; // Remember to delete [] retVal to not leak memory }
MFString MFString::GetExtension() const { int dot = FindCharReverse('.'); if(dot > FindCharReverse('/') && dot > FindCharReverse('\\')) return SubStr(dot); return MFString(); }
void TEST01_Sqstr_main() { SqString s, s1, s2, s3, s4; printf("(1)建立串s和串s1\n"); StrAssign(s, "abcdefghefghijklmn"); StrAssign(s1, "xyz"); printf("(2)输出串s:"); DispStr(s); printf("(3)串s的长度:%d\n", StrLength(s)); printf("(4)在串s的第9个字符位置插入串s1而产生串s2\n"); s2 = InsStr(s, 9, s1); printf("(5)输出串s2:"); DispStr(s2); printf("(6)删除串s第2个字符开始的5个字符而产生串s2\n"); s2 = DelStr(s, 2, 5); printf("(7)输出串s2:"); DispStr(s2); printf("(8)将串s第2个字符开始的5个字符替换成串s1而产生串s2\n"); s2 = RepStr(s, 2, 5, s1); printf("(9)输出串s2:"); DispStr(s2); printf("(10)提取串s的第2个字符开始的10个字符而产生串s3\n"); s3 = SubStr(s, 2, 10); printf("(11)输出串s3:"); DispStr(s3); printf("(12)将串s1和串s2连接起来而产生串s4\n"); s4 = Concat(s1, s2); printf("(13)输出串s4:"); DispStr(s4); }
MFString& MFString::Trim(bool bFront, bool bEnd, const char *pCharacters) { if(pData) { const char *pSrc = pData->pMemory; size_t offset = 0; // trim start if(bFront) { while(pSrc[offset] && MFString_Chr(pCharacters, pSrc[offset])) ++offset; } size_t count = pData->bytes - offset; // trim end if(bEnd) { const char *pEnd = pSrc + offset + count - 1; while(count && MFString_Chr(pCharacters, *pEnd)) { --count; --pEnd; } } *this = SubStr((int)offset, (int)count); } return *this; }
// ----------------------------------------------------------------------------- // TXmlParser::ElementBegin() // Parses beginning tag (eg. <General>) // ----------------------------------------------------------------------------- // TBool TXmlParser::ElementBegin() { TPtrC str=Str(); if (iSlash!=KErrNotFound) { return EFalse; } // remove possible attributes TInt pos=str.Locate(' '); if (pos==KErrNotFound) { pos=iLast; } TPtrC ptr=SubStr(iFirst, pos); if (!ValidTag(ptr)) { return EFalse; } iType=EElementBegin; iTag.Set(ptr); return ETrue; }
// Insert // Takes two arguments // An int – the index in this MyString // at which to insert the new chars // A MyString containing the chars to be inserted void MyString::Insert(const MyString & aMyString, int index) { if(index > _length) throw std::runtime_error("RAGEQUIT index is greater than Length()"); if(index < 0) throw std::runtime_error("RAGEQUIT index is less than zero"); MyString bMyString = aMyString; MyString returnString = MyString(); MyString subString = MyString(SubStr(index, _length - index)); returnString.Assign(SubStr(0, index)); returnString.Append(bMyString); returnString.Append(subString); Assign(returnString); }
char* MiniApache::ExtractRequestPath(){ int rlen = strlen(_HTTP_request); int start = FindChar(_HTTP_request, ' ', 0, rlen) + 1; int end = FindChar(_HTTP_request, '?', start, rlen); if (end == -1){ end = FindChar(_HTTP_request, ' ', start, rlen); } char * retVal = SubStr(_HTTP_request, start, end); return retVal; // Remember to delete [] retVal to not leak memory }
int main() { char *s; int FromPos, ToPos; s = strdup("Test"); FromPos = 2; ToPos = 3; printf("Result from SubStr: '%s'\n", SubStr(s, FromPos, ToPos)); return 0; }
char* MiniApache::ExtractRequestData(){ int rlen = strlen(_HTTP_request); int start = FindChar(_HTTP_request, '?', 0, rlen) + 1; int end = FindChar(_HTTP_request, ' ', start, rlen); if (start == 0){ start = end; } char * retVal = SubStr(_HTTP_request, start, end); // extract GET data return retVal; // Remember to delete [] retVal to not leak memory }
// ----------------------------------------------------------------------------- // TXmlParser::ElementAtt() // Parses attribute element (eg. <HW Version="1.5"/>) // Note: Attribute values cannot contain equals (=) or quotations (") // ----------------------------------------------------------------------------- // TBool TXmlParser::ElementAtt() { TPtrC str=Str(); TInt num1=Count(str, '='); TInt num2=Count(str, '"'); TInt pos=str.Locate(' '); // find end of tag if (iSlash==iLast-1 && num1>0 && num2==(2*num1) && pos!=KErrNotFound) { TPtrC ptr1=SubStr(iFirst, pos); if (!ValidTag(ptr1)) { return EFalse; } iType=EElementAtt; iTag.Set(ptr1); TPtrC ptr2=SubStr(pos, iSlash); iValue.Set(ptr2); return ETrue; } return EFalse; }
// ----------------------------------------------------------------------------- // TXmlParser::ElementValue() // Parses value (eg. <Language>en</Language>) // ----------------------------------------------------------------------------- // TBool TXmlParser::ElementValue() { TPtrC str=Str(); TInt pos1=str.Locate('>'); TInt pos2=str.LocateReverse('<'); if (pos1<pos2 && iSlash==pos2+1) { TPtrC ptr1=SubStr(iFirst, pos1); TPtrC ptr2=SubStr(iSlash, iLast); if (!ValidTag(ptr1) || ptr1.Compare(ptr2)!=0) { return EFalse; } iType=EElementValue; iTag.Set(ptr1); iValue.Set(SubStr(pos1, pos2)); return ETrue; } return EFalse; }
// ----------------------------------------------------------------------------- // TXmlParser::ElementEmpty() // Parses empty element (eg. <CaseSenN/>) // ----------------------------------------------------------------------------- // TBool TXmlParser::ElementEmpty() { if (iSlash!=iLast-1) { return EFalse; } TPtrC ptr=SubStr(iFirst, iSlash); if (!ValidTag(ptr)) { return EFalse; } iType=EElementEmpty; iTag.Set(ptr); return ETrue; }
// ----------------------------------------------------------------------------- // TXmlParser::ElementEnd() // Parses ending tag (eg. </General>) // ----------------------------------------------------------------------------- // TBool TXmlParser::ElementEnd() { if (iSlash!=iFirst+1) { return EFalse; } TPtrC ptr=SubStr(iSlash, iLast); if (!ValidTag(ptr)) { return EFalse; } iType=EElementEnd; iTag.Set(ptr); return ETrue; }
bool RedString::LineAtNum(const unsigned LineNum, RedString& Line) const { unsigned iLineStartIndex, iLineLength, iCurrIndex; unsigned iOnLine; // initialise the return value Line = ""; // initialise the working values iLineLength = 0; iCurrIndex = 0; iOnLine = 1; // Lines are indexed as we would want to see a position marker, from 1. // check we have enough lines to fullfill operation if ((LineNum > NumLines()) || (LineNum < 1)) return false; // Loop to the first character of the line we need while (iOnLine < LineNum) { if ( data[iCurrIndex] == '\n' ) iOnLine++; iCurrIndex++; } // mark the start of the line iLineStartIndex = iCurrIndex; // loop to the end of the line while ( (iCurrIndex<contentsize) && (data[iCurrIndex]!='\n') ) { iCurrIndex++; iLineLength++; } // use the inbuilt routine to retrieve the section of line Line = SubStr(iLineStartIndex, iLineLength); return true; }
static void TestStr() { CommentLine("Test String"); EQ (1, 1); NEQ(1, 232); char * testphrase = "Hello world."; char * str = NewStr(testphrase); NEQ(strlen(str), 0 ); NEQ(LenStr(str), 0 ); EQ (strlen(str), LenStr(str)); EQ (strcmp(testphrase, str), 0); NEQ(str, NULL); DelStr(str); char * tmp = (char *)malloc(sizeof(char) * strlen(str)); sprintf(tmp, "%s", testphrase); String buf = NewStr(tmp); EQ (strcmp(buf, testphrase), 0); String sub = SubStr(tmp, 0, 4); printf ("%s:%d %s:%d\n", buf, LenStr(buf), sub, LenStr(sub)); DelStr(buf); DelStr(sub); }
void main() { LinkString *s1, *s2, *s3, *s4, *s5, *s6, *s7; Assign(s1, "abcd"); printf("s1:"); DispStr(s1); printf("s1的长度:%d\n", StrLength(s1)); printf("s1=>s2\n"); StrCopy(s2, s1); printf("s2:"); DispStr(s2); printf("s1和s2%s\n", (StrEqual(s1, s2) == 1 ? "相同" : "不相同")); Assign(s3, "12345678"); printf("s3:"); DispStr(s3); printf("s1和s3连接=>s4\n"); s4 = Concat(s1, s3); printf("s4:"); DispStr(s4); printf("s3[2..5]=>s5\n"); s5 = SubStr(s3, 2, 4); printf("s5:"); DispStr(s5); Assign(s6, "567"); printf("s6:"); DispStr(s6); printf("s6在s3中位置:%d\n", Index(s3, s6)); printf("从s3中删除s3[3..6]字符\n"); DelStr(s3, 3, 4); printf("s3:"); DispStr(s3); printf("从s4中将s6替换成s1=>s7\n"); s7 = RepStrAll(s4, s6, s1); printf("s7:"); DispStr(s7); }
// Right most number of characters WString WString::Right ( int iCount ) const { return SubStr ( (int)length () - iCount, iCount ); }
// Left most number of characters WString WString::Left ( int iCount ) const { return SubStr ( 0, iCount ); }
//compute word probabilities void m_ovgraf::WordProbs(string &word){ int i,j,k; /////////////Word Probs///////// int restlen = MainData::WordLen - MainData::order; string wordrest; // wordrest = word[K+1,|word|] wordrest.resize(restlen); vector<vector<int>> RPList; //for each w in OV(HH), RPList(w) is list h in HH s.t. w=rpred(h) RPList.resize(NodeOv::NumOVNodes); //Remark. Words in RPList(w) have prefix order vector<int> RPSizes; //RPSizes(w) is size of RPList(w) RPSizes.resize(NodeOv::NumOVNodes); vector<int> KNumLinks; //for each word t of length K, KNumLinks(w) is number of right deep nodes r s.t. exists h in HH having prefix t and rpred(h) = t KNumLinks.resize(MModel_Prob::Power); vector<int> Prefixes; //for each h in HH, Prefixes(h) - prefix of length K of h Prefixes.resize(MainData::NWords); vector<int> Suffixes; //for each h in HH, Suffixes(h) - suffix of length K of h Suffixes.resize(MainData::NWords); ////////Create RPList; Suffixes; Prefixes////////////////// for(i = 0; i < MainData::NWords; i++){ H_M_Node* RP = static_cast<H_M_Node*>(Nodes[RLeafPreds[i]]); RPSizes[RP->num] ++; int pos1 = PosNLeafWords[i]; SubStr(WLeafWords, pos1, MainData::WordLen, word); int prefcd = MModel_Prob::PrefixN(MainData::order, word); int sufcd = MModel_Prob::SuffixN(MainData::order, word, MainData::WordLen); Prefixes[i] = prefcd; Suffixes[i] = sufcd; ////////////////Compute probabilities Prob(H~(r),q), H~(r) - set of motif words h s.t. t = rpred(h); record the probability in <r,q>.ProbMark /////////////////////////////////////// double prob = MModel_Prob::TermProb(word, MainData::WordLen, sufcd); int pos_in_matrix = MainData::WordLen - RP->len - 1; if(RP->len >= MainData::order){ RP->States[0]->ProbMark[pos_in_matrix] += prob; }else{ int pow = MModel_Prob::NumPower(MainData::AlpSize,RP->len); int rppos = sufcd/pow; RP->States[rppos]->ProbMark[pos_in_matrix] += prob; } } for(i = 0; i < NodeOv::NumOVNodes; i++){ RPList[i].reserve(RPSizes[i]); } RPSizes.clear(); for(i = 0; i < MainData::NWords; i++){ H_M_Node* RP = static_cast<H_M_Node*>(Nodes[RLeafPreds[i]]); RPList[RP->num].push_back(i); } ////////////////Create KNumLinks//////////////////////////// bool* Flags = Malloc<bool>(MModel_Prob::Power); for(i = 0; i < NodeOv::NumOVNodes; i++){ int s = RPList[i].size(); memset(Flags, 0x00, MModel_Prob::Power * sizeof(bool)); for(j = 0; j < s; j++){ int nword = RPList[i][j]; int prefcd = Prefixes[nword]; if(Flags[prefcd] == 0){ KNumLinks[prefcd] ++; Flags[prefcd] = 1; } } } free(Flags); Flags = nullptr; ///////////////////Initialization of WordProbs data structures (see MTrTree.h)/////////////////////// for(i = 0; i < MModel_Prob::Power; i++){ if(KNodes[i] != nullptr){ KNodes[i]->NumWLinks = 0; KNodes[i]->WLinks = Malloc<H_M_Node*>(KNumLinks[i]); typedef PriorList* PriorListPtr; KNodes[i]->WProbs = new PriorListPtr[KNumLinks[i]](); KNodes[i]->NumWProbs = Malloc<int>(KNumLinks[i]); } } KNumLinks.clear(); ////////////////Compute Word Probabilities, for each r in OV(HH) computing of Sum_{h in HH, r = rpred(h)}(Prob(pref_K(h),h[K+1,|h|],suf_K(h)))////////////////////////////////////////// //let pref_K(h) (suf_K(h)) be prefix (suffix) of h of length K double* Probs = Malloc<double>(MModel_Prob::Power); int curprefcd, prefcd, s, pow, sufcd, rppos,nword, numprobs; H_M_Node* RP; KM_TrTree* knode; double prob; for(i = 0; i < NodeOv::NumOVNodes; i++){ //for r in OV(HH) RP = static_cast<H_M_Node*>(Nodes[i]); s = (int)RPList[i].size(); pow = MModel_Prob::NumPower(MainData::AlpSize, RP->len); j = 0; while(j < s){ //loop of h in RPList(r) nword = RPList[i][j]; curprefcd = Prefixes[RPList[i][j]]; prefcd = curprefcd; knode =KNodes[prefcd]; //node in MTRTree cooresponding prefixes of h of length K knode->NumWLinks ++; knode->WLinks[knode->NumWLinks -1] = RP; memset(Probs, 0x00, RP->NStates * sizeof(double)); numprobs = 0; while((curprefcd == prefcd)&&(j < s)){ //loop on h in RPList(r) having the same prefix t of length K int pos1 = PosNLeafWords[nword] + MainData::order; SubStr(WLeafWords, pos1, restlen, wordrest); sufcd = Suffixes[nword]; if(RP->len >= MainData::order) rppos = 0; else rppos = sufcd/pow; prob = MModel_Prob::TermCondProb(prefcd, wordrest, restlen, sufcd); //computing of Prob(t,h[K+1,|h|],suf_K(h)) /////OSHIBKA?////// if((Probs[rppos] == 0)&&(prob != 0)) numprobs ++; // if(Probs[rppos] != 0) numprobs ++; Probs[rppos] += prob; j++; if(j < s){ nword = RPList[i][j]; prefcd = Prefixes[nword]; } } //fill word probs to t.WProbs knode->NumWProbs[knode->NumWLinks -1] = numprobs; knode->WProbs[knode->NumWLinks -1] = new PriorList[numprobs]; int l = 0; for(k = 0; k < RP->NStates; k++){ if(Probs[k] != 0){ knode->WProbs[knode->NumWLinks -1][l].pos = k; knode->WProbs[knode->NumWLinks -1][l].prob = Probs[k]; l++; } } } } free(Probs); Probs = nullptr; }
void m_ovgraf::States_and_BackProbs(H_M_Node* node, H_M_Node* LPnode, string &back, string &word){ int i,j; if(node->num == 0){ //if w is root node->States = Malloc<H_M_State*>(H_M_Node::NumAllStates); //initialization of w.States node->NStates = H_M_Node::NumAllStates; for(i = 0; i < H_M_Node::NumAllStates; i++){ node->States[i] = new H_M_State; node->States[i]->ID = i; } } else{ int pos1 = PosNNodeBacks[node->num]; int backlen = node->len - LPnode->len; SubStr(WNodeBacks,pos1,backlen,back); //take Back(w) for(i = 0; i < backlen; i++) word[LPnode->len + i] = back[i]; //take word w vector<int> statesid; statesid.reserve(H_M_Node::NumAllStates); MModel_Prob::CalcReachStates(word, node->len, statesid); //statesid contains ids of states from AllState(w) node->NStates = (int)statesid.size(); node->States = Malloc<H_M_State*>(node->NStates); for(i = 0; i < node->NStates; i++) node->States[i] = new H_M_State; ////////Back Probs/////////; int priorsize; //|PriorState(w,q)| if(node->len >= MainData::order) //if |w| >= K priorsize = LPnode->NStates; else priorsize = MModel_Prob::NumPower(MainData::AlpSize, backlen); int pow = MModel_Prob::Power/priorsize; int pow1 = MModel_Prob::NumPower(MainData::AlpSize, LPnode->len); int lpstate, pos; double prob; for(i = 0; i < node->NStates; i++){ // for all q in AllStates(w) node->States[i]->NumBackProbs = priorsize; node->States[i]->BackProbs = Malloc<PriorList>(priorsize); int state = statesid[i]; int cd = state/priorsize; node->States[i]->ID = state; if(node->len >= MainData::order){ //if |w|>K for(j = 0; j < LPnode->NStates; j++){ //for all q' in AllState(lpred(w)) lpstate = LPnode->States[j]->ID; //q' prob = MModel_Prob::TermCondProb(lpstate,back,backlen, state); //compute Prob(q',Back(w),q) node->States[i]->BackProbs[j].pos = j; node->States[i]->BackProbs[j].prob = prob; } } else{ for(j = 0; j < priorsize; j++){ lpstate = j*pow + cd; pos = lpstate/pow1; //position q' int lpred(w).States prob = MModel_Prob::TermCondProb(lpstate,back,backlen, state); //compute Prob(q',Back(w),q) node->States[i]->BackProbs[j].pos = pos; node->States[i]->BackProbs[j].prob = prob; } } } } /*if(node->num != 0)*/ for( i = 0; i < node->NLChilds; i++){ H_M_Node* LC = static_cast<H_M_Node*>(node->LChilds[i]); States_and_BackProbs(LC,node,back,word); } }
// ----------------------------------------------------------------------------- // TXmlParser::SubStr(TInt pos1, TInt pos2) // Function returns a sub-string between aPos1 and aPos2 // ----------------------------------------------------------------------------- // TPtrC TXmlParser::SubStr(TInt pos1, TInt pos2) { TPtrC str=Str(); return SubStr(str, pos1, pos2); }