Difference between revisions of "LazUtils"

From Free Pascal wiki
Jump to navigationJump to search
 
(24 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
== Overview ==
 +
LazUtils is collection of units that provides non-visual utility functions/classes; it is part of the units supplied by Lazarus but not the LCL (Lazarus Component Library). LazUtils do not have a dependancy on LCL, so they are also suitable for use in command-line, non-GUI applications.
 +
 
__TOC__
 
__TOC__
  
== TDictionaryStringList ==
+
=== TLookupStringList ===
 +
 
 +
The standard TStringList has a serious limitation about duplicated items, Duplicates property only works for sorted lists. It is not possible to avoid duplication in a list which is not sorted or has a custom sort.
 +
 
 +
TLookupStringList is an unsorted ''TStringList'' with a fast lookup feature. Internally it uses a string map container to store the string again. It is then used for ''InsertItem'', ''Contains'', ''IndexOf'' and ''Find'' methods. The extra container does not reserve excessive memory because the strings are reference-counted and not actually copied.
 +
 
 +
''TLookupStringList'' fully supports all Duplicates property values including ''dupIgnore'' and ''dupError''. A normal unsorted ''TStringList'' lacks this support for some values of Duplicates.
 +
 
 +
This class is particularly useful when you need to preserve the order of strings at the same time doing fast lookups (to check if a string is already there), or when you need to prevent addition of duplicate strings.
 +
 
 +
For a simple dedupe task, just load the strings you want to dedupe and it is done.
 +
 
 +
It is very memory efficient, because:
 +
* Internally it uses a balanced tree container (TStringMap) to store the strings again. A balanced tree is memory efficient compared to a hash map.
 +
* Strings have reference count and lazy copy semantics. It means only a reference to a string is copied to the other container.
 +
 
 +
You can find an example for TLookupStringList in your Lazarus folder: {LazarusDir}\components\lazutils\examples.
 +
 
 +
=== UTF8WrapText function ===
 +
 
 +
It wraps UTF8 text provided as a string into a MaxCol length. It inserts LineEndings at every last space/tab/hyphen character before MaxCol value or just on it and returns the resulting string.
 +
 
 +
== See also ==
  
This is an unsorted StringList with a fast lookup feature. Internally it uses a string map container to store the string again. It is then used for InserItem, Contains, IndexOf and Find methods. The extra container does not reserve too much memory because the strings are reference counted and not really copied. All Duplicates property values are fully supported, including dupIgnore and dupError, unlike in unsorted StringList.
+
* [[LazUtils Documentation Roadmap]]
 +
* [[LazFileUtils]]
  
This class is useful only when you must preserve the order in list, but also need to do fast lookups to see if a string exists, or must prevent duplicates.
+
[[Category:Lazarus]]
 +
[[Category:LazUtils]]

Latest revision as of 16:13, 20 July 2021

Overview

LazUtils is collection of units that provides non-visual utility functions/classes; it is part of the units supplied by Lazarus but not the LCL (Lazarus Component Library). LazUtils do not have a dependancy on LCL, so they are also suitable for use in command-line, non-GUI applications.

TLookupStringList

The standard TStringList has a serious limitation about duplicated items, Duplicates property only works for sorted lists. It is not possible to avoid duplication in a list which is not sorted or has a custom sort.

TLookupStringList is an unsorted TStringList with a fast lookup feature. Internally it uses a string map container to store the string again. It is then used for InsertItem, Contains, IndexOf and Find methods. The extra container does not reserve excessive memory because the strings are reference-counted and not actually copied.

TLookupStringList fully supports all Duplicates property values including dupIgnore and dupError. A normal unsorted TStringList lacks this support for some values of Duplicates.

This class is particularly useful when you need to preserve the order of strings at the same time doing fast lookups (to check if a string is already there), or when you need to prevent addition of duplicate strings.

For a simple dedupe task, just load the strings you want to dedupe and it is done.

It is very memory efficient, because:

  • Internally it uses a balanced tree container (TStringMap) to store the strings again. A balanced tree is memory efficient compared to a hash map.
  • Strings have reference count and lazy copy semantics. It means only a reference to a string is copied to the other container.

You can find an example for TLookupStringList in your Lazarus folder: {LazarusDir}\components\lazutils\examples.

UTF8WrapText function

It wraps UTF8 text provided as a string into a MaxCol length. It inserts LineEndings at every last space/tab/hyphen character before MaxCol value or just on it and returns the resulting string.

See also