Documentation
¶
Overview ¶
Copyright © 2023, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Copyright © 2023, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Package `hashtags` implements a simple #hashtag/@mentions handler.
Copyright © 2019, 2024 M.Watermann, 10247 Berlin, Germany All rights reserved EMail : <support@mwat.de>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.
Copyright © 2019, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Copyright © 2019, 2025 M.Watermann, 10247 Berlin, Germany
....All rights reserved EMail : <support@mwat.de>
Copyright © 2019, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Copyright © 2019, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Index ¶
- Constants
- Variables
- func HashMentionRE() *regexp.Regexp
- type TCountItem
- type TCountList
- func (cl TCountList) Compare(aList TCountList) int
- func (cl TCountList) Equal(aList TCountList) bool
- func (cl *TCountList) Insert(aItem TCountItem) *TCountList
- func (cl TCountList) Len() int
- func (cl TCountList) Less(aList TCountList) bool
- func (cl TCountList) String() (rStr string)
- func (cl *TCountList) Swap(aOldIdx, aNewIdx int) *TCountList
- type THashTagError
- type THashTags
- func (ht *THashTags) Checksum() uint32
- func (ht *THashTags) Clear() *THashTags
- func (ht *THashTags) Filename() string
- func (ht *THashTags) HashAdd(aHash string, aID int64) bool
- func (ht *THashTags) HashCount() int
- func (ht *THashTags) HashLen(aHash string) int
- func (ht *THashTags) HashList(aHash string) []int64
- func (ht *THashTags) HashRemove(aHash string, aID int64) bool
- func (ht *THashTags) IDlist(aID int64) []string
- func (ht *THashTags) IDparse(aID int64, aText []byte) bool
- func (ht *THashTags) IDremove(aID int64) bool
- func (ht *THashTags) IDrename(aOldID, aNewID int64) bool
- func (ht *THashTags) IDupdate(aID int64, aText []byte) bool
- func (ht *THashTags) Len() int
- func (ht *THashTags) LenTotal() int
- func (ht *THashTags) List() TCountList
- func (ht *THashTags) Load() (*THashTags, error)
- func (ht *THashTags) MentionAdd(aMention string, aID int64) bool
- func (ht *THashTags) MentionCount() int
- func (ht *THashTags) MentionLen(aMention string) int
- func (ht *THashTags) MentionList(aMention string) []int64
- func (ht *THashTags) MentionRemove(aMention string, aID int64) bool
- func (ht *THashTags) SetFilename(aFilename string) error
- func (ht *THashTags) Store() (int, error)
- func (ht *THashTags) String() string
Constants ¶
const ( // `MarkHash` is the first character in a `#hashtag`. MarkHash = byte('#') // `MarkMention` is the first character in a `@mention`. MarkMention = byte('@') )
Variables ¶
var ( // `UseBinaryStorage` determines whether to use binary storage // or not (i.e. plain text). // // Loading/storing binary data is about three times as fast with // the `THashTags` data than reading and parsing plain text data. UseBinaryStorage = true )
Functions ¶
func HashMentionRE ¶ added in v0.9.5
`HashMentionRE()` returns a compiled regular expression used to identify `#hashtags` and `@mentions` in a text.
This regular expression matches strings that start with either '@' or '#' followed by any number of characters that are not whitespace.
Returns:
- `*regexp.Regexp`: A pointer to the compiled regular expression.
Types ¶
type TCountItem ¶
type TCountItem struct { Count int // number of IDs for this `#hashtag` Tag string // name of `#hashtag` }
`TCountItem` holds a `#hashtag` and its number of occurrences.
func (TCountItem) Compare ¶ added in v0.9.6
func (ci TCountItem) Compare(aItem TCountItem) int
`Compare()` compares two `TCountItem` instances based on their tags and counts.
Parameters:
- `aItem`: The other TCountItem instance to compare with.
Returns:
- `-1` if the current instance is less than `aItem`.
- ` 0` if the current instance is equal to `aItem`.
- `+1` if the current instance is greater than `aItem`.
func (TCountItem) Equal ¶ added in v0.9.6
func (ci TCountItem) Equal(aItem TCountItem) bool
`Equal()` checks whether the current `TCountItem` is equal to `aItem`.
Parameters:
- `aItem`: The other item to compare with.
Returns:
- `bool`: Whether the two items are equal.
func (TCountItem) Less ¶ added in v0.7.0
func (ci TCountItem) Less(aItem TCountItem) bool
`Less()` checks whether this `TCountItem` is less than `aItem`.
Parameters:
- `aItem`: The other `TCountItem` instance to compare with.
Returns:
- `bool`: Whether the current instance is less than the other item.
type TCountList ¶ added in v0.7.0
type TCountList []TCountItem
`TCountList` is a list of `TCountItems`.
func (TCountList) Compare ¶ added in v0.9.6
func (cl TCountList) Compare(aList TCountList) int
`Compare()` compares the current list with another list.
Parameters:
- `aList`: The list to compare with.
Returns:
- `-1` if the current instance is less than `aList`.
- ` 0` if the current instance is equal to `aList`.
- `+1` if the current instance is greater than `aList`.
func (TCountList) Equal ¶ added in v0.9.6
func (cl TCountList) Equal(aList TCountList) bool
`Equal()` compares the current list with another list.
Parameters:
- `aList`: The list to compare with.
Returns:
- `bool`: True if the lists are identical, false otherwise.
func (*TCountList) Insert ¶ added in v0.9.1
func (cl *TCountList) Insert(aItem TCountItem) *TCountList
`Insert()` appends `aItem` to the list.
Parameters:
- `aItem`: The source ID to insert into the list.
func (TCountList) Len ¶ added in v0.9.6
func (cl TCountList) Len() int
func (TCountList) Less ¶ added in v0.9.6
func (cl TCountList) Less(aList TCountList) bool
`Less()` checks whether this `TCountItem` is less than `aList`.
Parameters:
- `aList`: The other `TCountList` instance to compare with.
Returns:
- `bool`: Whether the current instance is less than the other item.
func (TCountList) String ¶ added in v0.7.0
func (cl TCountList) String() (rStr string)
`String()` returns the list as a linefeed separated string.
(Implements `fmt.Stringer` interface)
Returns:
- `string`: The string representation of this list.
func (*TCountList) Swap ¶ added in v0.7.0
func (cl *TCountList) Swap(aOldIdx, aNewIdx int) *TCountList
`Swap()` swaps the elements at the specified indices in the list.
If the list is empty, or the old and new indices are the same, or the function, or either of the indices is out of bounds, the function returns the list unchanged.
Parameters:
- `aOldIdx`: The index of the first element to swap.
- `aNewIdx`: The index of the second element to swap.
Returns:
- `*TCountList`: A pointer to the list with the swapped elements.
type THashTagError ¶ added in v0.11.0
type THashTagError struct { Op string // operation that caused the error (e.g., "Load", "Store") Path string // file path involved in the error, if applicable Err error // underlying error that occurred }
`THashTagError` is a custom error type that provides detailed error information for hashtag-related operations.
func (*THashTagError) Error ¶ added in v0.11.0
func (e *THashTagError) Error() string
`Error()` implements the error interface, returning a formatted error message.
Returns:
- `string`: A formatted error message containing the operation, path (if any), and underlying error.
func (*THashTagError) Unwrap ¶ added in v0.11.0
func (e *THashTagError) Unwrap() error
`Unwrap()` returns the underlying error.
Returns:
- `error`: The underlying error that caused this `THashTagError`.
type THashTags ¶ added in v0.8.0
type THashTags struct {
// contains filtered or unexported fields
}
`THashTags` is a list of `#hashtags` and `@mentions` pointing to sources (i.e. IDs).
func New ¶
`New()` returns a new `THashTags` instance after reading the given file.
NOTE: An empty filename or if the hash file doesn't exist is not considered an error.
If there is an error, it will be of type `*THashTagError`.
Parameters:
- `aFilename`: The name of the file to use for loading and storing.
Returns:
- `*THashTags`: The new `THashTags` instance.
- `error`: If there is an error, it will be from reading `aFilename`.
func (*THashTags) Checksum ¶ added in v0.8.0
`Checksum()` returns the list's CRC32 checksum.
This method can be used to get a kind of 'footprint' of the current contents of the handled data.
Returns:
- `uint32`: The computed checksum.
func (*THashTags) Clear ¶ added in v0.8.0
`Clear()` empties the internal data structures: all `#hashtags` and `@mentions` are deleted.
Returns:
- `*THashTags`: This cleared list.
func (*THashTags) Filename ¶ added in v0.8.0
`Filename()` returns the configured filename for reading/storing this list's contents.
Returns:
- `string`: The filename for reading/storing this list.
func (*THashTags) HashAdd ¶ added in v0.8.0
`HashAdd()` appends `aID` to the list of `aHash`.
If `aHash` is empty it is silently ignored (i.e. this method does nothing) returning `false`.
Parameters:
- `aHash`: The hash list index to use.
- `aID`: The object to be added to the hash list.
Returns:
- `bool`: `true` if `aID` was added, or `false` otherwise.
func (*THashTags) HashCount ¶ added in v0.8.0
`HashCount()` counts the number of hashtags in the list.
Returns:
- `int`: The number of hashes in the list.
func (*THashTags) HashLen ¶ added in v0.8.0
`HashLen()` returns the number of IDs stored for `aHash`.
If `aHash` is empty it is silently ignored (i.e. this method does nothing), returning `-1`.
Parameters:
- `aHash`: The list key to lookup.
Returns:
- `int`: The number of `aHash` in the list.
func (*THashTags) HashList ¶ added in v0.8.0
`HashList()` returns a list of IDs associated with `aHash`.
If `aHash` is empty it is silently ignored (i.e. this method does nothing), returning an empty slice.
Parameters:
- `aHash`: The hash to lookup.
Returns:
- `[]int64`: The number of references of `aHash`.
func (*THashTags) HashRemove ¶ added in v0.8.0
`HashRemove()` deletes `aID` from the list of `aHash`.
Parameters:
- `aHash`: The hash to lookup.
- `aID`: The referenced object to remove from the list.
Returns:
- `bool`: `true` if `aID` was removed, or `false` otherwise.
func (*THashTags) IDlist ¶ added in v0.8.0
`IDlist()` returns a list of `#hashtags` and `@mentions` associated with `aID`.
Parameters:
- `aID`: The referenced object to lookup.
Returns:
- `[]string`: The list of `#hashtags` and `@mentions` associated with `aID`.
func (*THashTags) IDparse ¶ added in v0.8.0
`IDparse()` checks whether `aText` associated with `aID` contains strings starting with `[@|#]` and - if found - adds them to the respective list.
If `aText` is empty it is silently ignored (i.e. this method does nothing), returning `false`.
Parameters:
- `aID`: The ID to add to the list.
- `aText:` The text to search.
Returns:
- `bool`: `true` if `aID` was updated from `aText`, or `false` otherwise.
func (*THashTags) IDremove ¶ added in v0.8.0
`IDremove()` deletes all `#hashtags` and `@mentions` associated with `aID`.
Parameters:
- `aID`: The ID to be deleted from all lists.
Returns:
- `bool`: `true` if `aID` was removed, or `false` otherwise.
func (*THashTags) IDrename ¶ added in v0.8.0
`IDrename()` replaces all occurrences of `aOldID` by `aNewID`.
If `aOldID` equals `aNewID` they are silently ignored (i.e. this method does nothing), returning `false`.
This method is intended for rare cases when the ID of a document needs to get changed.
Parameters:
- `aOldID`: The ID to be replaced in all lists.
- `aNewID`: The replacement in all lists.
Returns:
- `bool`: `true` if `aOldID` was renamed, or `false` otherwise.
func (*THashTags) IDupdate ¶ added in v0.8.0
`IDupdate()` checks `aText` removing all `#hashtags` and `@mentions` no longer present and adding `#hashtags` and `@mentions` new in `aText`.
Parameters:
- `aID`: The ID to update.
- `aText`: The new text to use.
Returns:
- `bool`: `true` if `aID` was updated, or `false` otherwise.
func (*THashTags) Len ¶ added in v0.8.0
`Len()` returns the current length of the list i.e. how many `#hashtags` and `@mentions` are currently stored in the list.
Returns:
- `int`: The number of all `#hashtag` and `@mention` lists.
func (*THashTags) LenTotal ¶ added in v0.8.0
`LenTotal()` returns the length of all `#hashtag` and `@mention` lists stored in the list.
Returns:
- `int`: The total length of all `#hashtag` and `@mention` lists.
func (*THashTags) List ¶ added in v0.8.0
func (ht *THashTags) List() TCountList
`List()` returns a list of `#hashtags` and `@mentions` with their respective count of associated IDs.
Returns:
- `TCountList`: A list of `#hashtags` and `@mentions` with their counts of IDs.
func (*THashTags) Load ¶ added in v0.8.0
`Load()` reads the configured file returning the data structure read from the file and a possible error condition.
NOTE: An empty filename or the hash file doesn't exist that is not considered an error.
Returns:
- `*THashTags`: The updated list.
- `error`: If there is an error, it will be of type `*PathError`.
func (*THashTags) MentionAdd ¶ added in v0.8.0
`MentionAdd()` appends `aID` to the list of `aMention`.
If `aMention` is empty it is silently ignored (i.e. this method does nothing) returning `false`.
Parameters:
- `aMention`: The list index to lookup.
- `aID`: The ID to be added to the hash list.
Returns:
- `bool`: `true` if `aID` was added, or `false` otherwise.
func (*THashTags) MentionCount ¶ added in v0.8.0
`MentionCount()` returns the number of mentions in the list.
Returns:
- `int`: The number of mentions in the list.
func (*THashTags) MentionLen ¶ added in v0.8.0
`MentionLen()` returns the number of IDs stored for `aMention`.
If `aMention` is empty it is silently ignored (i.e. this method does nothing) returning `-1`.
Parameters:
- `aMention`: Identifies the ID list to lookup.
Returns:
- `int`: The number of `aMention` in the list.
func (*THashTags) MentionList ¶ added in v0.8.0
`MentionList()` returns a list of IDs associated with `aMention`.
If `aMention` is empty it is silently ignored (i.e. this method does nothing), returning an empty slice.
Parameters:
- `aMention`: The mention to lookup.
Returns:
- `[]int64`: The number of references of `aMention`.
func (*THashTags) MentionRemove ¶ added in v0.8.0
`MentionRemove()` deletes `aID` from the list of `aMention`.
If `aMention` is empty it is silently ignored (i.e. this method does nothing) returning `false`.
Parameters:
- `aMention`: The mention to lookup.
- `aID`: The referenced object to remove from the list.
Returns:
- `bool`: `true` if `aID` was removed, or `false` otherwise.
func (*THashTags) SetFilename ¶ added in v0.8.0
`SetFilename()` sets `aFilename` to be used by this list.
Parameters:
- `aFilename`: The name of the file to use for storage.
Returns:
- `error`: If there is an error, it will be of type `*HashTagError`.
func (*THashTags) Store ¶ added in v0.8.0
`Store()` writes the whole list to the configured file returning the number of bytes written and a possible error.
If there is an error, it will be of type `*THashTagError`.
Returns:
- `int`: Number of bytes written to storage.
- `error`: A possible storage error, or `nil` in case of success.