Diary: I made a class today. The name of the class is jstring. It is used to perform conventional operations on the string. This class is characterized by almost all JStrings to be referenced in JString & Form. From the idea of managed memory management, the variable can be freely created and released. All objects created will be released automatically. So how long it will be written in one line. ~~ There is no need to manually allocate buffer. . ~~~~ My level is not high. I hope the high finger is positive.
The downloaded place is: http://www.lostinet.com/public/cplusplus/jstring/http://www.lostinet.com/public/cplusplus/jstring/jstring.cpp.txtttp://www.lostinet.com / /public/cplusplus/jstring/jstring.zip
Related 9CBS Forum Articles: http://www.9cbs.net/expert/topic/984/984638.xml? Temp = .7542993
// Testlib.cpp: Defines the entry point for the console application.//
#include "stdafx.h"
/ ************************************************** *************** / chain commissioned by the memory management of the stack string class JString head heap tail by- http://www.lostinet.com author:? Lostinet [lost network]. . (Remember to remember me ..) / ************************************************* *********************** /
#ifndef _lostinet_class_jstring_ # define _lostinet_class_jstring_
/ * Because I am a jscript fan, and depending on the String type in JScript, this name is jstring.
Working Principle: Define a JString object allocated by stack within a function. Then when the function returns, the JString object will be released. The object that the JString object is generated by all of the operations, connects with a chain. Then when the JString object generated by Stack is released, all JString it is automatically released. Therefore, using the JString * pointer outside the class is an error of the use and the object in a chain is preferably not passed to other functions.
The usual method is to define the first JString JS in a function; then other variables use JString & Types
Not recommended to pass JString *, and JString &
Advice: Use JString * outside outside. Never pay attention to the life of the chain, especially the JString & JString & time of different chains, returned JString & belongs to the first chain
* / # include
/ ************************************************** *************** /
INT getLength () {RETURN Length;} int indexof (const char * str, int start = 0) {if (start 1> length) Return-1; char * postart = pstring start; char * pos = strstr (PStart , STR); if (POS == 0) Return -1; return pos-pstring;} int LastIndexof (const char * str) {int LEN = Strlen (STR); char * buf1 = new char [length 1]; Char * buf2 = new char [g 1]; strcpy (buf1, pstring); strcpy (buf2, str); strrev (buf1); strrev (buf2); char * pOS = strstr (buf1, buf2); delete buf1; DELETE BUF2; IF (POS == 0) RETURN-1; RETURN LENGTH BUF1-POS-LEN;} JString & Left (INT LEN) {Return Substr (0, LEN);} JString & Right (int LEN) {Return Substr Length-len, len);
JString & Substr (INT Start, INT LEN) {IF (Start <0) START = 0; if (start 1> length) Return * New JString (* Pallocator); if (Len <= 0) Return * New JString (* Pallocator); if (start len> length) len = length-start; char * buf = new char [len 1]; Memcpy (buf, pstring start, len); buf [len] = 0; jstring * PSTR = New JString (* Pallocator, Len, BUF); delete buf; return * pstr;}
JString & Lower () {jstring * pstr = new jstring (* pallocator); strcpy (pstr-> getBuffersetLength (length), pstring); strlwr (pstr-> getBuffer ()); return * pstr;}
JString & Upper () {jstring * pstr = new jstring (* pallocator); strcpy (pstr-> getBuffersetLength (length), pstring; strupr (pstr-> getBuffer ()); return * pstr;} / ***** *********************************************************** ********* /
Double Number () {Return Strtod (pstring, 0);}
JString & Convert (BOOL B) {RETURN * New JSTRING (* Pallocator, B? 4: 5, B? "True": "false");} jstring & convert (double d) {char Buf [64]; sprintf (buf, "% g", d); return * new jstring (* Pallocator, Strlen (buf), buf); JString & Convert (int N) {char Buf [64]; Sprintf (buf, "% d", n); Return * New JString (* Pallocator, Strlen (BUF), BUF); JString & Convert (const char * str) {Return * New JString (* Pallocator, Strlen (Str), STR);} / ****** *********************************************************** ******** /
// All char * input, the address must not be 0
JString & Operator (JString & Str) {//if (str.pallocator! =pallocator )thRow ("JString& Operator (JString & Str) cannot use different chains"); // Note: The elements of the two chains add The return element belongs to the first chain jstring * Pres = new jstring (* Pallocator, Length str.length, pstring); strcat (pres-> pstring, str.pstring); return.com;} JString & Operator (Const) Char * STR) {JString * Pres = New JString (* Pallocator, Length Strlen (Str), PString; strcat (Pres-> pstring, str); return * pres;} jstring & operator (bool b) {jstring * PSTR = New JSTRING (* Pallocator, Length (B? 4: 5), pstring; strcat (pstr-> pstring, (b? "true": "false")); return * pstr;} JString & Operator (Double d) {char Buf [64]; sprintf (buf, "% g", d); return * this buf;} jstring & operator (int N) {char Buf [64]; sprintf (buf, "% d" , RETURN * this buf;} jstring & operator = (BOOL B) {* this = B? "true": "false"; return * this;} jstring & operator = (double d) {char buf [ 64]; Sprintf (BUF, "% G", D); * this = buf; return * this;} jstring & operator = (int N) {char Buf [64]; sprintf (buf, "% D) ", N); * this = buf; return * this;} jstring & operator = (const char * str) {length = length strlen (str); char * pstr = new char [Length 1]; strcpy (PSTR , pstring; strcat (pstr, str); delete pstring; pstring = pstr; return * this;} jstring & operator = (BOOL B) {if (b) * this = "true"; else * this = "false"; Return * this;} jstring & operator = (double d) {charf [64]; sprintf (buf, "% g", d); * this = buf; return * this;
} Jstring & operator = (int N) {char Buf [64]; sprintf (buf, "% d", n); * this = buf; return * this;} jstring & operator = (jstring & str) {delete pstring; length = Str.Length; pstring = new char [length 1]; strcpy (pstring, str.pstring); return * this;} jstring & operator = (const char * str) {delete pstring; length = strlen (str); pstring = NEW Char [Length 1]; STRCPY (PString, Str); Return * this;} / ************************************** ****************************************** /
Bool Comparenocase (const char * str) {return strcmpi (pstring, str) == 0;}
Bool Operator == (const char * str) {Return strcmp (pstring, str) == 0;} BOOL OPERATOR == (int N) {charf [64]; sprintf (buf, "% d", n); Return * this == buf;} BOOL OPERATOR == (Double D) {Char BUF [64]; Sprintf (buf, "% g", d); return * this == buf;}
/ ************************************************** *************** /
Operator Bool () {RETURN LENGTH> 0? True: false;
Operator const char * () {return pstring;}}; template
#ENDIF / / _ LOSTINET_CLASS_JSTRING_
#include "iostream.h" int main (int Argc, char * argv []) {// If you get Szurl // const char * szurl = "http://www.lostinet.com/some.asp "; // const char * szurl =" http://www.lostinet.com/some.asp?id=35&style=1 "; const char * szurl =" http://www.lostinet.com/some.asp ? id = 35 & Page = 3 & Style = 1 & Page = 5 ";
// The target is to parse the string, take it out of 3 in Page = 3, default is 1 // If it is a general action, then take a long code // note, this process does not explicitly assign buffer // Remove the annotation, no more Szurl, then a total of four variables // wherein characters are related to JString Jsurl and JString & JSPARAMS // code total 22 rows (including 3 {and 3}) INT Page = 1; JString Jsurl (SZURL); / / Search the place in the URL? If there is no question mark, the representative does not have a parameter int pos = jsurl.indexof ("?"); If (pOS> -1) // indexof returns -1 represents the search is not "?", No parameters {// jstring & jsparams = is obtained The reference to the new JString object // POS 1 is "?" //Jsurl.getlength () is the length of JSURL, Substr automatically recognizes the length of JSURL. Take the string last // lower () gets lowercase, non-regular ways are strlwr (jsparams.getbuffer ()); jstring & jsparams = jsurl.substr (POS 1, jsurl.getlength ()). Lower (); // Overloaded (BOOL) operator, meaning while (jsparams.getlength ()> 1) While (jsparams) {// Start looking for drop POS = jsparams.indexof ("Page ="); // Can't find "Page =" Of course, IF (POS == - 1) Break; // 0 is because JSParams first is Page = .... // The case should continue if IF (POS == 0 ) {// Remove "Page =" and then get the number of numbers after // jstring ("3 & Style = 1 & Page = 5") .Number () is 3 Page = Jsparams.SUBSTR (5, jsparams.getlength ()). Number ); If Break; // does not break, you will continue to search. In the example, obtain the URL parameter split POS = jsparams.indexof ("&"); // If there is no split, the JSParams representing this time is the last one. IF (POS == - 1) Break; // "ID = 35 & Page = 3 & Style = 1 & Page = 5" => "Page = 3 & Style = 1 & Page = 5" // So push jsparams = jsparams.substr (POS 1, JSparams. GetLength ());}} cout << Page;
// Performance Reference: // After the code is running, the JString object allocates 4, each accounting for 20Byte's memory. Jsurl is the first // process is: // 1 jstring (): jstring jsurl (szurl); // 2 subs (): jstring & jsparams = jsurl.substr (POS 1, jsurl.getlength ()). Lower ( ); // 3 Lower (): 同 // 4 SUBSTR (): jsparams = jsparams.substr (POS 1, jsparams.getLength ()); // 5 Substr (): Page = jsparams.substr (5, jsparams . Getlength ()). Number (); // If Break; // does not break, you will continue to search. In the example, get it = 5 // removed, then the JString object allocates 8. // Because more JSParams = jsparams.substr (POS 1, jsparams.getlength ()); // jstring must wait for the function of its link to exit, the entire chain will be released. // If a function does not quit long, constantly calculates the string, then the number of operators should be determined (10,000 operations are not much, 100,000 times), // That takes JString / / cout << "/ n / N / N / N"; return 0;}
-------------------------------------------------- ---------
Example After removing the comment: #include "iostream.h" int main (int Argc, char * argv []) {const char * szurl = "http://www.lostinet.com/some.asp?id=35&page=3&terstyle = 1 & Page = 5 ";
INT page = 1; jstring jsurl; int pos = jsurl.indexof ("?"); if (pOS> -1) {jstring & jsparams = jsurl.substr (POS 1, jsurl.getlength ()). Lower (); While (jsparams) {POS = jsparams.indexof ("PAGE ="); if (POS == - 1) Break; if (POS == 0) {Page = jsparams.substr (5, JSparams.Getlength ))))). Number (); break;} POS = jsparams.indexof ("&"); if (pOS == - 1) Break; jsparams = jsparams.substr (POS 1, jsparams.getlength;}} Cout << page; cout << "/ n / n / n / n"; return 0;}