Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
10 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. SUB_STRING
  5. Abstract:
  6. This module contains the definition of the SUB_STRING class.
  7. Environment:
  8. ULIB, User Mode
  9. Notes:
  10. A substring is a subsequence of characters taken from a "base"
  11. string. Substrings are useful in those cases in which we want
  12. to operate on "chunks" of a given string (e.g. parsing), without
  13. having to create separate strings for each one.
  14. One must be careful when using substrings, though. Since
  15. substrings depend on a base string, the base string must exist
  16. during the life span of all its substrings. In other words,
  17. the base string must not be deleted before having deleted all
  18. the substrings obtained from it.
  19. Note that GENERIC_STRINGs have no substring support. It is up to
  20. the derived string classes to provide it.
  21. Since substrings are just "windows" into a base string, any
  22. modification of a substring will be reflected in the base
  23. string (hence in all other substrings). This is why we have
  24. two classes of substrings:
  25. SUB_STRING allows only read operations. Note however that
  26. one can modify the base string directly.
  27. DYNAMIC_SUB_STRING is derived from the above, and it allows
  28. write operations. Needless to say, you should
  29. not use this class unless you're sure of what
  30. you are doing.
  31. Substring objects cannot be created and initialized directly by the
  32. user, they must be obtained thru a base string class with substring
  33. support.
  34. Substrings pertaining to the same base string are chained together
  35. in a doubly-linked queue. Since substrings have no knowledge of the
  36. internals of their base string, the chain has a dummy element as its
  37. head. In order to provide substring support, the base string must
  38. follow these steps:
  39. 1.- Before spawning any substring, the substring chain head must
  40. be created and initialized using the InitializeChainHead() method.
  41. Note that this substring is a dummy one, beknown only to the base
  42. string.
  43. 2.- Whenever spawning a new substring, it must be linked to the chain
  44. using the InitializeChainNode() method. Any substring already in
  45. the chain may be use as argument, but the obvious choice is the
  46. chain head.
  47. 3.- Each time that the size of the string changes, the Update() of the
  48. chain head must be invoked.
  49. 4.- Before destroying a base string, you have to make sure that all its
  50. substrings have gone away. This means that the chain head must be
  51. the only element of the chain. Use the HasLinks() method to
  52. verify this.
  53. 5.- Don't forget to destroy the chain head before destorying the base
  54. string.
  55. --*/
  56. //
  57. // This class is no longer supported
  58. //
  59. #include "wstring.hxx"
  60. #define _SUB_STRING_
  61. #if !defined (_SUB_STRING_)
  62. #define _SUB_STRING_
  63. #include "string.hxx"
  64. DECLARE_CLASS( SUB_STRING );
  65. class SUB_STRING : public GENERIC_STRING {
  66. friend SUB_STRING;
  67. public:
  68. DECLARE_CONSTRUCTOR( SUB_STRING );
  69. DECLARE_CAST_MEMBER_FUNCTION( SUB_STRING );
  70. VIRTUAL
  71. ~SUB_STRING (
  72. );
  73. NONVIRTUAL
  74. BOOLEAN
  75. HasLinks (
  76. );
  77. NONVIRTUAL
  78. BOOLEAN
  79. InitializeChainHead (
  80. IN PGENERIC_STRING BaseString
  81. );
  82. NONVIRTUAL
  83. BOOLEAN
  84. InitializeChainNode (
  85. IN OUT PSUB_STRING SubString,
  86. IN CHNUM Position,
  87. IN CHNUM Length
  88. );
  89. VIRTUAL
  90. PBYTE
  91. GetInternalBuffer (
  92. IN CHNUM Position DEFAULT 0
  93. ) CONST;
  94. VIRTUAL
  95. BOOLEAN
  96. IsChAt (
  97. IN WCHAR Char,
  98. IN CHNUM Position DEFAULT 0
  99. ) CONST;
  100. VIRTUAL
  101. BOOLEAN
  102. MakeNumber (
  103. OUT PLONG Number,
  104. IN CHNUM Position DEFAULT 0,
  105. IN CHNUM Length DEFAULT TO_END
  106. ) CONST;
  107. VIRTUAL
  108. ULONG
  109. QueryByteCount (
  110. IN CHNUM Position DEFAULT 0,
  111. IN CHNUM Length DEFAULT TO_END
  112. ) CONST;
  113. VIRTUAL
  114. WCHAR
  115. QueryChAt(
  116. IN CHNUM Position DEFAULT 0
  117. ) CONST;
  118. VIRTUAL
  119. CHNUM
  120. QueryChCount (
  121. ) CONST;
  122. VIRTUAL
  123. PGENERIC_STRING
  124. QueryGenericString (
  125. IN CHNUM Position DEFAULT 0,
  126. IN CHNUM Length DEFAULT TO_END
  127. ) CONST;
  128. VIRTUAL
  129. PSTR
  130. QuerySTR(
  131. IN CHNUM Position DEFAULT 0,
  132. IN CHNUM Length DEFAULT TO_END,
  133. IN OUT PSTR Buffer DEFAULT NULL,
  134. IN ULONG BufferSize DEFAULT 0
  135. ) CONST;
  136. VIRTUAL
  137. PSUB_STRING
  138. QuerySubString (
  139. IN CHNUM Position DEFAULT 0,
  140. IN CHNUM Length DEFAULT TO_END,
  141. OUT PSUB_STRING SubString DEFAULT NULL
  142. );
  143. VIRTUAL
  144. PWSTR
  145. QueryWSTR (
  146. IN CHNUM Position DEFAULT 0,
  147. IN CHNUM Length DEFAULT TO_END,
  148. IN OUT PWSTR Buffer DEFAULT NULL,
  149. IN ULONG BufferSize DEFAULT 0,
  150. IN BOOLEAN ForceNull DEFAULT TRUE
  151. ) CONST;
  152. VIRTUAL
  153. BOOLEAN
  154. Replace (
  155. IN PCGENERIC_STRING String2,
  156. IN CHNUM Position DEFAULT 0,
  157. IN CHNUM Length DEFAULT TO_END,
  158. IN CHNUM Position2 DEFAULT 0,
  159. IN CHNUM Length2 DEFAULT TO_END
  160. );
  161. VIRTUAL
  162. BOOLEAN
  163. SetChAt (
  164. IN WCHAR Char,
  165. IN CHNUM Position DEFAULT 0,
  166. IN CHNUM Length DEFAULT TO_END
  167. );
  168. VIRTUAL
  169. CHNUM
  170. Strchr (
  171. IN WCHAR Char,
  172. IN CHNUM Position DEFAULT 0,
  173. IN CHNUM Length DEFAULT TO_END
  174. ) CONST;
  175. VIRTUAL
  176. LONG
  177. Strcmp (
  178. IN PCGENERIC_STRING GenericString
  179. ) CONST;
  180. VIRTUAL
  181. CHNUM
  182. Strcspn (
  183. IN PCGENERIC_STRING GenericString,
  184. IN CHNUM Position DEFAULT 0,
  185. IN CHNUM Length DEFAULT TO_END
  186. ) CONST;
  187. VIRTUAL
  188. LONG
  189. Stricmp (
  190. IN PCGENERIC_STRING GenericString
  191. ) CONST;
  192. VIRTUAL
  193. LONG
  194. StringCompare (
  195. IN CHNUM Position1,
  196. IN CHNUM Length1,
  197. IN PCGENERIC_STRING GenericString2,
  198. IN CHNUM Position2,
  199. IN CHNUM Length2,
  200. IN USHORT CompareFlags DEFAULT COMPARE_IGNORECASE
  201. ) CONST;
  202. VIRTUAL
  203. CHNUM
  204. StrLen (
  205. ) CONST;
  206. VIRTUAL
  207. CHNUM
  208. Strrchr (
  209. IN WCHAR Char,
  210. IN CHNUM Position DEFAULT 0,
  211. IN CHNUM Length DEFAULT TO_END
  212. ) CONST;
  213. VIRTUAL
  214. CHNUM
  215. Strspn (
  216. IN PCGENERIC_STRING GenericString,
  217. IN CHNUM Position DEFAULT 0,
  218. IN CHNUM Length DEFAULT TO_END
  219. ) CONST;
  220. VIRTUAL
  221. CHNUM
  222. Strstr (
  223. IN PCGENERIC_STRING GenericString,
  224. IN CHNUM Position DEFAULT 0,
  225. IN CHNUM Length DEFAULT TO_END
  226. ) CONST;
  227. NONVIRTUAL
  228. BOOLEAN
  229. Update (
  230. IN CHNUM Length
  231. );
  232. protected:
  233. NONVIRTUAL
  234. PGENERIC_STRING
  235. GetBaseString (
  236. );
  237. NONVIRTUAL
  238. PSUB_STRING
  239. GetNextInChain (
  240. );
  241. NONVIRTUAL
  242. PSUB_STRING
  243. GetPreviousInChain (
  244. );
  245. NONVIRTUAL
  246. VOID
  247. GetValidLength(
  248. IN CHNUM Position,
  249. IN OUT PCHNUM Length
  250. ) CONST;
  251. NONVIRTUAL
  252. CHNUM
  253. QueryStartingPosition (
  254. ) CONST;
  255. NONVIRTUAL
  256. VOID
  257. SetChCount(
  258. IN CHNUM NewCount
  259. );
  260. private:
  261. NONVIRTUAL
  262. VOID
  263. Construct (
  264. );
  265. NONVIRTUAL
  266. VOID
  267. Destroy (
  268. );
  269. PGENERIC_STRING _BaseString; // base string to take substring
  270. CHNUM _StartingPosition; // starting index within base
  271. CHNUM _CountOfChars; // # of chars in substring
  272. PSUB_STRING _Previous; // Previous in chain
  273. PSUB_STRING _Next; // Next in chain
  274. #if DBG==1
  275. ULONG _Signature;
  276. BOOLEAN _Initialized;
  277. #endif
  278. };
  279. INLINE
  280. PGENERIC_STRING
  281. SUB_STRING::GetBaseString (
  282. )
  283. /*++
  284. Routine Description:
  285. Gets a pointer to the base string
  286. Arguments:
  287. none
  288. Return Value:
  289. Pointer to the base string
  290. --*/
  291. {
  292. DebugAssert( _Initialized );
  293. return _BaseString;
  294. }
  295. INLINE
  296. PSUB_STRING
  297. SUB_STRING::GetNextInChain (
  298. )
  299. /*++
  300. Routine Description:
  301. Gets a pointer to next substring in the chain
  302. Arguments:
  303. none
  304. Return Value:
  305. Pointer to substring
  306. --*/
  307. {
  308. DebugAssert( _Initialized );
  309. return _Next;
  310. }
  311. INLINE
  312. PSUB_STRING
  313. SUB_STRING::GetPreviousInChain (
  314. )
  315. /*++
  316. Routine Description:
  317. Gets a pointer to previous substring in the chain
  318. Arguments:
  319. none
  320. Return Value:
  321. Pointer to the previous substring
  322. --*/
  323. {
  324. DebugAssert( _Initialized );
  325. return _Previous;
  326. }
  327. INLINE
  328. VOID
  329. SUB_STRING::GetValidLength (
  330. IN CHNUM Position,
  331. IN OUT PCHNUM Length
  332. ) CONST
  333. /*++
  334. Routine Description:
  335. If the length passed has the magic value TO_END, then it is
  336. updated to be the from the passed position up to the end of the
  337. substring.
  338. Arguments:
  339. Position - Supplies a starting position within the substring
  340. Length - Supplies a length
  341. Return Value:
  342. none
  343. --*/
  344. {
  345. DebugAssert(_Initialized );
  346. DebugAssert( Position <= _CountOfChars );
  347. if ( *Length == TO_END ) {
  348. *Length = _CountOfChars - Position;
  349. }
  350. DebugAssert( Position + *Length <= _CountOfChars );
  351. }
  352. INLINE
  353. BOOLEAN
  354. SUB_STRING::HasLinks (
  355. )
  356. /*++
  357. Routine Description:
  358. Determines if the substring has forward and backward links
  359. Arguments:
  360. none
  361. Return Value:
  362. TRUE if the stubstring has forward or backward links
  363. FALSE otherwise
  364. --*/
  365. {
  366. DebugAssert( _Initialized );
  367. return ( (GetNextInChain() != NULL ) || (GetPreviousInChain() != NULL ) );
  368. }
  369. INLINE
  370. CHNUM
  371. SUB_STRING::QueryStartingPosition (
  372. ) CONST
  373. /*++
  374. Routine Description:
  375. Returns the starting character position within the base string.
  376. Arguments:
  377. None.
  378. Return Value:
  379. The starting character position within the base string.
  380. --*/
  381. {
  382. DebugAssert( _Initialized );
  383. return _StartingPosition;
  384. }
  385. INLINE
  386. VOID
  387. SUB_STRING::SetChCount (
  388. IN CHNUM NewCount
  389. )
  390. /*++
  391. Routine Description:
  392. Sets the number of characters in the substring
  393. Arguments:
  394. NewCount - Supplies the new count of characters
  395. Return Value:
  396. none
  397. --*/
  398. {
  399. DebugAssert( _Initialized );
  400. _CountOfChars = NewCount;
  401. }
  402. #endif // _SUB_STRING_
  403. /*++
  404. Copyright (c) 1990 Microsoft Corporation
  405. Module Name:
  406. DYNAMIC_SUB_STRING
  407. Abstract:
  408. This module contains the definition of the DYNAMIC_SUB_STRING class.
  409. Notes:
  410. DYNAMIC_SUB_STRINGs can modify the base string (see notes for
  411. SUB_STRING class).
  412. Revision History:
  413. --*/
  414. #define _DYNAMIC_SUB_STRING_
  415. #if !defined (_DYNAMIC_SUB_STRING_)
  416. #define _DYNAMIC_SUB_STRING_
  417. DECLARE_CLASS( DYNAMIC_SUB_STRING );
  418. class DYNAMIC_SUB_STRING : public SUB_STRING {
  419. public:
  420. DECLARE_CONSTRUCTOR( DYNAMIC_SUB_STRING );
  421. DECLARE_CAST_MEMBER_FUNCTION( DYNAMIC_SUB_STRING );
  422. NONVIRTUAL
  423. BOOLEAN
  424. Copy (
  425. IN PCGENERIC_STRING GenericString
  426. );
  427. VIRTUAL
  428. BOOLEAN
  429. SetChAt (
  430. IN WCHAR Char,
  431. IN CHNUM Position DEFAULT 0,
  432. IN CHNUM Length DEFAULT TO_END
  433. );
  434. NONVIRTUAL
  435. CHNUM
  436. Truncate (
  437. IN CHNUM Position DEFAULT 0
  438. );
  439. private:
  440. VOID
  441. Construct (
  442. );
  443. };
  444. #endif // _DYNAMIC_SUB_STRING_