1 /*
2  * This file contains source code for the PyMOL computer program
3  * Copyright (c) Schrodinger, LLC.
4  *
5  * Handling of "interned" strings in PyMOLGlobals::Lexicon
6  */
7 
8 #pragma once
9 
10 #include "PyMOLGlobals.h"
11 #include "OVLexicon.h"
12 #include "pymol/zstring_view.h"
13 
14 #define LexDec(G, i) OVLexicon_DecRef(G->Lexicon, i)
15 #define LexInc(G, i) OVLexicon_IncRef(G->Lexicon, i)
16 #define LexNumeric(i) (i)
17 
18 /*
19  * Get the pointer to the internal string storage for reference `i`.
20  */
LexStr(PyMOLGlobals * G,const lexidx_t & i)21 inline const char * LexStr(PyMOLGlobals * G, const lexidx_t & i) {
22   return (i) ? OVLexicon_FetchCString(G->Lexicon, i) : "";
23 }
24 
25 /*
26  * Lookup or insert new string `s` into the global lexicon and return
27  * it's numerical reference. Will always return 0 for the empty string.
28  */
LexIdx(PyMOLGlobals * G,pymol::zstring_view s)29 inline lexidx_t LexIdx(PyMOLGlobals * G, pymol::zstring_view s) {
30   return s && !s.empty() ? OVLexicon_GetFromCString(G->Lexicon, s.c_str()).word : 0;
31 }
32 
33 /*
34  * Assignment (i = j) with reference count update for old and new value.
35  */
LexAssign(PyMOLGlobals * G,lexidx_t & i,const lexidx_t & j)36 inline void LexAssign(PyMOLGlobals * G, lexidx_t& i, const lexidx_t& j) {
37   if (i != j) {
38     LexDec(G, i);
39     i = j;
40     LexInc(G, i);
41   }
42 }
43 
LexAssign(PyMOLGlobals * G,lexidx_t & i,const char * s)44 inline void LexAssign(PyMOLGlobals * G, lexidx_t& i, const char * s) {
45   LexDec(G, i);
46   i = LexIdx(G, s);
47 }
48 
49 #define LEX_BORROW_NOTFOUND -1
50 
51 /*
52  * Lookup string `s` without inserting or incrementing the ref count. If
53  * `s` is not in the lexicon, return -1.
54  */
LexBorrow(PyMOLGlobals * G,const char * s)55 inline lexidx_t LexBorrow(PyMOLGlobals * G, const char * s) {
56   auto result = OVLexicon_BorrowFromCString(G->Lexicon, s);
57   return (result.status == OVstatus_SUCCESS) ? result.word : LEX_BORROW_NOTFOUND;
58 }
59