summaryrefslogtreecommitdiffstats
path: root/private/utils/ulib/inc/substrng.hxx
blob: c7faa054255848835b5d40bfa4d8a6065bcdc35c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    SUB_STRING

Abstract:

    This module contains the definition of the SUB_STRING class.

Author:

    Steve Rowe (stever)

Environment:

    ULIB, User Mode

Notes:

	  A substring is a subsequence of characters taken from a "base"
	string. Substrings are useful in those cases in which we want
	to operate on "chunks" of a given string (e.g. parsing), without
	having to create separate strings for each one.

	  One must be careful when using substrings, though. Since
	substrings depend on a base string, the base string must exist
	during the life span of all its substrings. In other words,
	the base string must not be deleted before having deleted all
	the substrings obtained from it.

	  Note that GENERIC_STRINGs have no substring support. It is up to
	the derived string classes to provide it.

	  Since substrings are just "windows" into a base string, any
	modification of a substring will be reflected in the base
	string (hence in all other substrings). This is why we have
	two classes of substrings:

	SUB_STRING			allows only read operations. Note however that
						one can modify the base string directly.

	DYNAMIC_SUB_STRING	is derived from the above, and it allows
						write operations. Needless to say, you should
						not use this class unless you're sure of what
						you are doing.

	  Substring objects cannot be created and initialized directly by the
	user, they must be obtained thru a base string class with substring
	support.

	  Substrings pertaining to the same base string are chained together
	in a doubly-linked queue. Since substrings have no knowledge of the
	internals of their base string, the chain has a dummy element as its
	head.	In order to provide substring support, the base string must
	follow these steps:


	1.- Before spawning any substring, the substring chain head must
		be created and initialized using the InitializeChainHead() method.
		Note that this substring is a dummy one, beknown only to the base
		string.

	2.- Whenever spawning a new substring, it must be linked to the chain
		using the InitializeChainNode() method. Any substring already in
		the chain may be use as argument, but the obvious choice is the
		chain head.

	3.- Each time that the size of the string changes, the Update() of the
		chain head must be invoked.

	4.- Before destroying a base string, you have to make sure that all its
		substrings have gone away. This means that the chain head must be
		the only element of the chain.	Use the HasLinks() method to
		verify this.

	5.- Don't forget to destroy the chain head before destorying the base
		string.


--*/

//
// This class is no longer supported
//

#include "wstring.hxx"

#define _SUB_STRING_

#if !defined (_SUB_STRING_)

#define _SUB_STRING_

#include "string.hxx"

DECLARE_CLASS( SUB_STRING );

class SUB_STRING : public GENERIC_STRING {

	friend	SUB_STRING;

    public:

		DECLARE_CONSTRUCTOR( SUB_STRING );
		DECLARE_CAST_MEMBER_FUNCTION( SUB_STRING );

		VIRTUAL
		~SUB_STRING (
			);

		NONVIRTUAL
		BOOLEAN
		HasLinks (
			);

		NONVIRTUAL
		BOOLEAN
		InitializeChainHead (
			IN	PGENERIC_STRING 	BaseString
			);

		NONVIRTUAL
		BOOLEAN
		InitializeChainNode (
			IN OUT	PSUB_STRING		SubString,
			IN		CHNUM			Position,
			IN		CHNUM			Length
			);

		VIRTUAL
		PBYTE
		GetInternalBuffer (
			IN	CHNUM	Position DEFAULT 0
			) CONST;

		VIRTUAL
        BOOLEAN
        IsChAt (
			IN WCHAR	Char,
			IN CHNUM	Position	DEFAULT 0
            ) CONST;

		VIRTUAL
		BOOLEAN
		MakeNumber (
			OUT PLONG	Number,
			IN	CHNUM	Position	DEFAULT 0,
			IN	CHNUM	Length		DEFAULT TO_END
            ) CONST;

		VIRTUAL
		ULONG
		QueryByteCount (
			IN	CHNUM	Position	DEFAULT 0,
			IN	CHNUM	Length		DEFAULT TO_END
			) CONST;

		VIRTUAL
		WCHAR
        QueryChAt(
            IN CHNUM    Position DEFAULT 0
            ) CONST;

		VIRTUAL
        CHNUM
        QueryChCount (
            ) CONST;

		VIRTUAL
		PGENERIC_STRING
		QueryGenericString (
			IN	CHNUM		Position	DEFAULT 0,
			IN	CHNUM		Length		DEFAULT TO_END
			) CONST;

		VIRTUAL
		PSTR
		QuerySTR(
			IN		CHNUM	Position	DEFAULT 0,
			IN		CHNUM	Length		DEFAULT TO_END,
			IN OUT	PSTR	Buffer		DEFAULT NULL,
			IN		ULONG	BufferSize	DEFAULT 0
			) CONST;

		VIRTUAL
		PSUB_STRING
		QuerySubString (
			IN	CHNUM		Position	DEFAULT 0,
			IN	CHNUM		Length		DEFAULT TO_END,
			OUT PSUB_STRING SubString	DEFAULT NULL
			);

		VIRTUAL
		PWSTR
		QueryWSTR (
			IN		CHNUM	Position	DEFAULT 0,
			IN		CHNUM	Length		DEFAULT TO_END,
			IN OUT	PWSTR	Buffer		DEFAULT NULL,
            IN      ULONG   BufferSize  DEFAULT 0,
            IN      BOOLEAN ForceNull   DEFAULT TRUE
			) CONST;

		VIRTUAL
		BOOLEAN
		Replace (
			IN PCGENERIC_STRING	String2,
			IN CHNUM			Position	DEFAULT 0,
			IN CHNUM			Length		DEFAULT TO_END,
			IN CHNUM			Position2	DEFAULT 0,
			IN CHNUM			Length2 	DEFAULT TO_END
			);

		VIRTUAL
        BOOLEAN
        SetChAt (
			IN WCHAR	Char,
			IN CHNUM	Position DEFAULT 0,
			IN CHNUM	Length	 DEFAULT TO_END
			);

		VIRTUAL
		CHNUM
		Strchr  (
			IN	WCHAR	 Char,
			IN	CHNUM	 Position	DEFAULT 0,
			IN	CHNUM	 Length 	DEFAULT TO_END
			) CONST;

		VIRTUAL
		LONG
		Strcmp  (
			IN PCGENERIC_STRING 	GenericString
			) CONST;

		VIRTUAL
		CHNUM
		Strcspn (
			IN	PCGENERIC_STRING	GenericString,
			IN	CHNUM				Position		DEFAULT 0,
			IN	CHNUM				Length			DEFAULT TO_END
			) CONST;

		VIRTUAL
		LONG
		Stricmp (
			IN PCGENERIC_STRING		GenericString
			) CONST;

		VIRTUAL
		LONG
		StringCompare (
			IN CHNUM			Position1,
			IN CHNUM			Length1,
			IN PCGENERIC_STRING	GenericString2,
			IN CHNUM			Position2,
			IN CHNUM			Length2,
			IN USHORT			CompareFlags	DEFAULT COMPARE_IGNORECASE
			) CONST;


		VIRTUAL
		CHNUM
		StrLen (
			) CONST;

		VIRTUAL
		CHNUM
		Strrchr (
			IN	WCHAR	Char,
			IN	CHNUM	Position	DEFAULT 0,
			IN	CHNUM	Length		DEFAULT TO_END
			) CONST;

		VIRTUAL
		CHNUM
		Strspn  (
			IN PCGENERIC_STRING	GenericString,
			IN CHNUM			Position		DEFAULT 0,
			IN CHNUM			Length			DEFAULT TO_END
			) CONST;

		VIRTUAL
		CHNUM
		Strstr  (
			IN PCGENERIC_STRING	GenericString,
			IN CHNUM			Position		DEFAULT 0,
			IN CHNUM			Length			DEFAULT TO_END
			) CONST;

		NONVIRTUAL
        BOOLEAN
        Update (
			IN	CHNUM	Length
            );

   protected:

		NONVIRTUAL
		PGENERIC_STRING
		GetBaseString (
			);

		NONVIRTUAL
		PSUB_STRING
		GetNextInChain (
			);

		NONVIRTUAL
		PSUB_STRING
		GetPreviousInChain (
			);

		NONVIRTUAL
		VOID
		GetValidLength(
			IN		CHNUM	Position,
			IN OUT	PCHNUM	Length
			) CONST;

		NONVIRTUAL
		CHNUM
		QueryStartingPosition (
			) CONST;

		NONVIRTUAL
		VOID
		SetChCount(
			IN	CHNUM	NewCount
			);

	private:

		NONVIRTUAL
		VOID
		Construct (
			);

		NONVIRTUAL
		VOID
		Destroy (
			);

		PGENERIC_STRING	_BaseString;		// base string to take substring
		CHNUM			_StartingPosition;	// starting index within base
		CHNUM			_CountOfChars;		// # of chars in substring

		PSUB_STRING 	_Previous;			// Previous in chain
		PSUB_STRING 	_Next;				// Next in chain

#if DBG==1
		ULONG			_Signature;
		BOOLEAN 		_Initialized;
#endif

};

INLINE
PGENERIC_STRING
SUB_STRING::GetBaseString (
	)

/*++

Routine Description:

	Gets a pointer to the base string

Arguments:

	none

Return Value:

	Pointer to the base string

--*/

{
	DebugAssert( _Initialized );
	return _BaseString;
}

INLINE
PSUB_STRING
SUB_STRING::GetNextInChain (
	)

/*++

Routine Description:

	Gets a pointer to next substring in the chain

Arguments:

	none

Return Value:

	Pointer to substring

--*/

{
	DebugAssert( _Initialized );
	return _Next;
}

INLINE
PSUB_STRING
SUB_STRING::GetPreviousInChain (
	)

/*++

Routine Description:

	Gets a pointer to previous substring in the chain

Arguments:

	none

Return Value:

	Pointer to the previous substring

--*/

{
	DebugAssert( _Initialized );
	return _Previous;
}

INLINE
VOID
SUB_STRING::GetValidLength (
	IN		CHNUM	Position,
	IN OUT	PCHNUM	Length
	) CONST

/*++

Routine Description:

	If the length passed has the magic value TO_END, then it is
	updated to be the from the passed position up to the end of the
	substring.

Arguments:

	Position	-	Supplies a starting position within the substring
	Length		-	Supplies a length

Return Value:

	none

--*/

{

	DebugAssert(_Initialized );

	DebugAssert( Position <= _CountOfChars );

	if ( *Length == TO_END ) {
		*Length = _CountOfChars - Position;
	}

	DebugAssert( Position + *Length <= _CountOfChars );
}

INLINE
BOOLEAN
SUB_STRING::HasLinks (
	)

/*++

Routine Description:

	Determines if the substring has forward and backward links

Arguments:

	none

Return Value:

	TRUE  if the stubstring has forward or backward links
	FALSE otherwise

--*/

{
	DebugAssert( _Initialized );
	return ( (GetNextInChain() != NULL ) || (GetPreviousInChain() != NULL ) );

}

INLINE
CHNUM
SUB_STRING::QueryStartingPosition (
    ) CONST

/*++

Routine Description:

	Returns the starting character position within the base string.

Arguments:

    None.

Return Value:

	The starting character position within the base string.

--*/

{
	DebugAssert( _Initialized );
	return _StartingPosition;
}

INLINE
VOID
SUB_STRING::SetChCount (
	IN	CHNUM	NewCount
	)

/*++

Routine Description:

	Sets the number of characters in the substring

Arguments:

	NewCount	-	Supplies the new count of characters

Return Value:

	none

--*/

{
	DebugAssert( _Initialized );
	_CountOfChars = NewCount;
}



#endif	// _SUB_STRING_

/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    DYNAMIC_SUB_STRING

Abstract:

    This module contains the definition of the DYNAMIC_SUB_STRING class.

Author:

    steve rowe              stever          2/1/91

Notes:

    DYNAMIC_SUB_STRINGs can modify the base string (see notes for
    SUB_STRING class).


Revision History:

--*/

#define _DYNAMIC_SUB_STRING_

#if !defined (_DYNAMIC_SUB_STRING_)

#define _DYNAMIC_SUB_STRING_

DECLARE_CLASS( DYNAMIC_SUB_STRING );

class DYNAMIC_SUB_STRING : public SUB_STRING {

    public:

		DECLARE_CONSTRUCTOR( DYNAMIC_SUB_STRING );
		DECLARE_CAST_MEMBER_FUNCTION( DYNAMIC_SUB_STRING );

		NONVIRTUAL
		BOOLEAN
		Copy (
			IN PCGENERIC_STRING 	GenericString
            );

		VIRTUAL
        BOOLEAN
        SetChAt (
			IN WCHAR	Char,
			IN CHNUM	Position DEFAULT 0,
			IN CHNUM	Length	 DEFAULT TO_END
			);

		NONVIRTUAL
		CHNUM
		Truncate (
			IN	CHNUM	Position DEFAULT 0
			);

    private:

		VOID
		Construct (
			);

};


#endif	// _DYNAMIC_SUB_STRING_