Started by Juergen Kuehlwein, April 09, 2018, 11:39:00 PM
0 Members and 1 Guest are viewing this topic.
PRIVATE FUNCTION Remove_ overload (BYREF w AS WSTRING, byval anyflag as long = 0, BYREF m AS WSTRING, _ byval iflag as long = 0) AS ustringDIM u AS ustring = w
' ========================================================================================PRIVATE CONSTRUCTOR CBStr (BYREF bstrHandle AS AFX_BSTR = NULL, BYVAL fAttach AS LONG = TRUE) CBSTR_DP("--BEGIN CBSTR CONSTRUCTOR AFX_BSTR - handle: " & .WSTR(bstrHandle) & " - Attach: " & .WSTR(fAttach)) IF bstrHandle = NULL THEN m_bstr = SysAllocString("") CBSTR_DP("CBSTR CONSTRUCTOR SysAllocString - " & .WSTR(m_bstr)) ELSE ' Detect if the passed handle is an OLE string ' If it is an OLE string it must have a descriptor; otherwise, don't ' Get the length in bytes looking at the descriptor and divide by 2 to get the number of ' unicode characters, that is the value returned by the FreeBASIC LEN operator. DIM Res AS INTEGER = PEEK(DWORD, CAST(ANY PTR, bstrHandle) - 4) \ 2 ' If the retrieved length if the same that the returned by LEN, then it must be an OLE string IF Res = .LEN(*bstrHandle) AND fAttach <> FALSE THEN CBSTR_DP("CBSTR CONSTRUCTOR AFX_BSTR - Attach handle: " & .WSTR(bstrHandle)) ' Attach the passed handle to the class m_bstr = bstrHandle ELSE CBSTR_DP("CBSTR CONSTRUCTOR AFX_BSTR - Alloc handle: " & .WSTR(bstrHandle)) ' Allocate an OLE string with the contents of the string pointer by bstrHandle m_bstr = SysAllocString(*bstrHandle) END IF END IF CBSTR_DP("--END CBSTR CONSTRUCTOR AFX_BSTR - " & .WSTR(m_bstr))END CONSTRUCTOR' ========================================================================================
PRIVATE FUNCTION AfxStrReplace OVERLOAD (BYREF wszMainStr AS CONST WSTRING, BYREF wszMatchStr AS WSTRING, BYREF wszReplaceWith AS WSTRING) AS CWSTR DIM cwsMainStr AS CWSTR = wszMainStr
DIM cbs AS CBSTR = "1234567890"print AfxStrReplace(cbs, "5", "x")print cbs
FUNCTION somefunc(b as CBstr) AS LONGDIM b1 as CBstr = b...
QuoteOK - i didn´t know or at least i didn´t understand that!so, when getting passed a CBstr (or an OLE wide string) like thisCode: [Select]FUNCTION somefunc(b as CBstr) AS LONGDIM b1 as CBstr = b...b1 is not a copy of b (as i would expect) but is in fact b itself, because only the OLE handle has been copied and not the data. This means when b1 goes out of scope, b is destroyed as well.
PRIVATE CONSTRUCTOR CBStr (BYREF cbs AS CBStr) m_bstr = SysAllocString(cbs)END CONSTRUCTOR
Quoteif this is for special cases only, wouldn´t it have been better to have a special "attach" operator for exactly these special cases, instead of making it a standard behavior, which opens unexpected traps.
CONSTRUCTOR CBStr (BYREF bstrHandle AS AFX_BSTR = NULL, BYVAL fAttach AS LONG = TRUE)
Quotehow does PowerBASIC handle this situation?
QuoteI think, your approach was to have a separate OLE wide string type ONLY for COM, not only because it must be an OLE string, but also for implementing automatic freeing of the passed string handle. That is, when using CBstr with COM you don´t have to care about when to free passed strings and when not to - your CBstr does it automatically for you. Is this correct ?
QuoteThe reason i defined USTRING as CBstr (and not as CWstr, which of course is possible) is, that i hoped to have a "one for all" wide string type. A type which basically works everywhere, without having to makes decisions where to use this and where to use that. When it´s about heavy string manipulation and i want more speed i can always implement CWstr for this - that´s the idea behind it.
PRIVATE FUNCTION Remove_ overload (BYREF w AS USTRING, byval anyflag as long = 0, BYREF m AS WSTRING, _ byval iflag as long = 0) AS ustringDIM u AS ustring = w
QuoteIf you still have some hair in your head, you will lose it.
PRIVATE FUNCTION _StrRemove OVERLOAD (BYREF wszMainStr AS USTRING, BYREF wszMatchStr AS WSTRING) AS USTRING DIM ustr AS USTRING = wszMainStr DIM nLen AS LONG = LEN(wszMatchStr) DO DIM nPos AS LONG = INSTR(**ustr, wszMatchStr) IF nPos = 0 THEN EXIT DO ustr = MID(ustr, 1, nPos - 1) & MID(ustr, nPos + nLen) LOOP RETURN ustrEND FUNCTION
Quote Don't use USTRING defined as CWSTR with COM
#include once "Afx\CVAR.inc"PRIVATE FUNCTION StrRemove (BYREF cvMainStr AS CVAR, BYREF cvMatchStr AS CVAR) AS CVAR DIM cv AS CVAR = cvMainStr DIM nLen AS LONG = LEN(cvMatchStr.wstr) DO DIM nPos AS LONG = INSTR(cv, cvMatchStr) IF nPos = 0 THEN EXIT DO cv = MID(cv, 1, nPos - 1) & MID(cv, nPos + nLen) LOOP RETURN cvEND FUNCTIONprint StrRemove("Hello World. Welcome to the Freebasic World", "World")DIM s AS STRING = "Hello World. Welcome to the Freebasic World"PRINT StrRemove(s, "World")DIM cws AS CWSTR = "Hello World. Welcome to the Freebasic World"PRINT StrRemove(cws, "World")DIM cbs AS CBSTR = "Hello World. Welcome to the Freebasic World"PRINT StrRemove(cbs, "World")
DIM cv AS CVAR = "Hello World. Welcome to the Freebasic World"PRINT AfxStrRemove(cv, "World")
DIM cv AS CVAR = "Test string"cv = cv & " 123"cv = cv & 45cv += " - some more text"print cvPRINT LEFT(cv, 4)
DIM cv AS CVAR = "Test string"DIM cv2 AS CVAR = 12345.67print cv & " " & cv2
QuoteDIM cbs AS CBSTR, and pass cbs or cbs.sptr to IN parameters and cbs.vptr to OUT/INOUT parameters.
QuoteProcedures that expect a WSTRING retrieve the length searching for a double null, but procedures that expect a BSTR retrieve the length calling SysStringLen.
...property someprop(byref p as CBstr) as long ...dim cws as CWstr = "Hello"dim n as long ...n = someprop(cws)...
property someprop(byref p as CWstr) as long
Page created in 0.252 seconds with 20 queries.