String variable with limited number of characters - string

Can I create a variable of the type string with a limited number of characters/runes? Something similar to a byte array that only accepts n entries?
var myByteArray [20]byte
Can I even create a custom data type that will only accepts n entries?

Can I create a variable of the type string with a limited number of characters/ "Runes"?
No.

Can I even create a custom data type that will only accepts n entries?
You can create a type that contains exactly n entries. That's called an array in Go.
You can make an array of any other data type, including bytes or runes.
Perhaps something like this would be close to what you want:
type myRuneArray [20]rune
However, this does not have an upper limit of 20 runes, rather it always has 20 runes. This may or may not be useful for you, depending on your use case.

Related

Numeric values in YamlDotNet.RepresentationModel

How do I get numeric values from the RepresentationModel?
Say, after traversing a document, I have a YamlScalarNode. It has a string Value, which I can, of course, try to convert to a number, but I'd expect YAML to detect the type and present it as int or double etc. (perhaps via descendants from YamlScalarNode, whose type I could detect).
Is there an official way to do it that I'm missing?
Note that I can't use Serialization: the document structure does not directly map to a class; it can be a recursive definition of arbitrary depth, and the end values are either scalar numbers or sequences of numbers (vectors).
Also, can YamlDotNet handle numerical keys in mappings? This means that keys 1 and 01 should be considered duplicates. I believe YAML specification requires that, but I'm not certain...
The YAML schemas specify how scalars are to be interpreted. Ideally, you would look at the tag of a scalar to establish its type according to the selected schema. However, YamlDotNet does not yet implement them. For now you will have to do that yourself.

how to code a string to a unique number and decode it

Is there any way to code a long string to a unique number (integer) and then decode this number to original string? (I mean to reduce size of long string)
The simple answer is no.
The complex answer is maybe.
What you are looking for is compression, compression can reduce the size of the String but there is no guarantee as to how small it can make it. In particular you can never guarantee being able to fit it into a certain sized integer.
There are concepts like "hashing" which may help you do what you want depending on exactly what you are trying to do with this number.
Alternatively if you use the same string in a lot of different places then you can store it once and pass references/pointers to that single instance of the String around.
First you need to hash it to string eg md5. Then you convert the characters of the hash string into numbers according to the alphabetical number

Data Structure to use instead of hash_map

I want to make an array containing three wide character arrays such that one of them is the key.
"LPWCH,LPWCH,LPWCH" was not able to use the greater than/lesser than symbols since it thinks it is a tag
Hash_map only lets me use a pair. wKey and the element associated with it. Is there another data structure that lets me use this?
This set will be updated by different threads almost simultaneously. And thats the reason why I don't want to use a class or another struct to define the remaining two wide character arrays.
You can use LPWCH as a key and std::pair<LPWCH, LPWCH> as an element.
Using any of LP-typedefs is not good. You would only be comparing the points, and not strings.
LPWCH is nothing but a WCHAR* which can be drilled down to void*. When you compare two pointers, you are comparing where they are pointing, and not what they are pointing.
You either need to have another comparer attached to your map/hash_map, or use actual string datatype (like std::string, CString)

String data type in sqlite

I created a table as follows:
create table employers (
id integer primary key,
name string,
surname string,
description string
);
I noticed here that string is not between data types, should I replace it with something else? Is it alias for some other datatype? What is the maximum number of characters that can this string contain?
I used string and it does not make any problems. I'm only interested how is it possible, that I could use string. Is it some alias? What are the possibilities of this type?
thank you
You want to use TEXT which we use whatever it needs to in order to hold your data.
CREATE TABLE Employers (
id INTEGER PRIMARY KEY,
name TEXT,
surname TEXT,
description TEXT
);
See http://www.sqlite.org/datatype3.html and note:
SQLite uses a more general dynamic
type system. In SQLite, the datatype
of a value is associated with the
value itself, not with its container.
The dynamic type system of SQLite is
backwards compatible with the more
common static type systems of other
database engines in the sense that SQL
statement that work on statically
typed databases should work the same
way in SQLite. However, the dynamic
typing in SQLite allows it to do
things which are not possible in
traditional rigidly typed databases.
That noted:
SQLite does not impose any length
restrictions (other than the large
global SQLITE_MAX_LENGTH limit) on the
length of strings, BLOBs or numeric
values.
Finally:
Maximum length of a string or BLOB
The maximum number of bytes in a
string or BLOB in SQLite is defined by
the preprocessor macro
SQLITE_MAX_LENGTH. The default value
of this macro is 1 billion (1 thousand
million or 1,000,000,000). You can
raise or lower this value at
compile-time using a command-line
option like this:
-DSQLITE_MAX_LENGTH=123456789 The current implementation will only
support a string or BLOB length up to
231-1 or 2147483647. And some built-in
functions such as hex() might fail
well before that point. In
security-sensitive applications it is
best not to try to increase the
maximum string and blob length. In
fact, you might do well to lower the
maximum string and blob length to
something more in the range of a few
million if that is possible.
During part of SQLite's INSERT and
SELECT processing, the complete
content of each row in the database is
encoded as a single BLOB. So the
SQLITE_MAX_LENGTH parameter also
determines the maximum number of bytes
in a row.
The maximum string or BLOB length can
be lowered at run-time using the
sqlite3_limit(db,SQLITE_LIMIT_LENGTH,size)
interface.
Declare your field as "TEXT" and I believe you can specify a length restriction.
so you can declare name TEXT(255) as a 255 character TEXT value.

Data structure for storing strings?

I'm looking for a data structure to store strings in. I require a function in the interface that takes a string as its only parameter and returns a reference/iterator/pointer/handle that can be used to retrieve the string for the rest of the lifetime of the data structure. Set membership, entry deletion etc. is not required.
I'm more concerned with memory usage than speed.
One highly efficient data structure for storing strings is the Trie. This saves both memory and time by storing strings with common prefixes using the same memory.
You could use as the pointer returned the final marker of the string in the Trie, which uniquely identifies the string, and could be used to recreate the string by traversing the Trie upwards.
I think the keyword here is string interning, where you store only one copy of each distinct string. In Java, this is accomplished by String.intern():
String ref1 = "hello world".intern();
String ref2 = "HELLO WORLD".toLowerCase().intern();
assert ref1 == ref2;
I think the best bet here would be an ArrayList. The common implementations have some overhead from allocating extra space in the array for new elements, but if memory is such a requirement, you can allocate manually for each new element. It will be slower, but will only use the necessary memory for the string.
There are three ways to store strings:
Fixed length (array type structure)
Variable length but maximum size is fixed during running time (pointer type structure)
Linked list structure

Resources