make a pure string from a string literal

View: New views
4 Messages — Rating Filter:   Alert me  

make a pure string from a string literal

by Dan Nicolaescu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


There are a few build_string("STRING LITERAL") calls in emacs/src/*.c
The result is actually a constant string, so we could do:
Fpurecopy (build_string ("STRING LITERAL"))
but that creates another copy of "STRING LITERAL" in pure memory, we
already a have a perfectly good one in read only memory.

So how about adding:

Lisp_Object
make_pure_string_from_literal (const char *data)
{
  Lisp_Object string;
  struct Lisp_String *s;

  s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
  s->data = data;
  s->size = strlen (data);
  s->size_byte = -1;
  s->intervals = NULL_INTERVAL;
  XSETSTRING (string, s);
  return string;
}

Then we can use make_pure_string_from_literal ("STRING LITERAL");

Any objections?
(better suggestions for the function name are welcome)



Re: make a pure string from a string literal

by Juanma Barranquero :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 17:01, Dan Nicolaescu <dann@...> wrote:

> There are a few build_string("STRING LITERAL") calls in emacs/src/*.c
> The result is actually a constant string, so we could do:
> Fpurecopy (build_string ("STRING LITERAL"))

It is unlikely, but a build_string("STRING LITERAL") could have text
properties added later.

    Juanma



Re: make a pure string from a string literal

by Stefan Monnier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> So how about adding:

> Lisp_Object
> make_pure_string_from_literal (const char *data)

My local hacks include the hunk below, so I think it's a good idea, tho
I never got around to installing it.


        Stefan


@@ -4821,6 +4887,23 @@
   return string;
 }
 
+Lisp_Object
+make_pure_c_string (data)
+     const char *data;
+{
+  Lisp_Object string;
+  struct Lisp_String *s;
+  int nchars = strlen (data);
+
+  s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
+  s->inlined = 0;
+  s->size = nchars;
+  s->size_byte = -1;
+  s->data.ptr = data;
+  s->intervals = NULL_INTERVAL;
+  XSETSTRING (string, s);
+  return string;
+}
 
 /* Return a cons allocated from pure space.  Give it pure copies
    of CAR as car and CDR as cdr.  */



Re: make a pure string from a string literal

by Dan Nicolaescu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stefan Monnier <monnier@...> writes:

  > > So how about adding:
  >
  > > Lisp_Object
  > > make_pure_string_from_literal (const char *data)
  >
  > My local hacks include the hunk below, so I think it's a good idea, tho
  > I never got around to installing it.

Please do.
I'd be happy to install it for you (and make use of it).


  >         Stefan
  >
  >
  > @@ -4821,6 +4887,23 @@
  >    return string;
  >  }
  >  
  > +Lisp_Object
  > +make_pure_c_string (data)
  > +     const char *data;
  > +{
  > +  Lisp_Object string;
  > +  struct Lisp_String *s;
  > +  int nchars = strlen (data);
  > +
  > +  s = (struct Lisp_String *) pure_alloc (sizeof *s, Lisp_String);
  > +  s->inlined = 0;
  > +  s->size = nchars;
  > +  s->size_byte = -1;
  > +  s->data.ptr = data;
  > +  s->intervals = NULL_INTERVAL;
  > +  XSETSTRING (string, s);
  > +  return string;
  > +}
  >  
  >  /* Return a cons allocated from pure space.  Give it pure copies
  >     of CAR as car and CDR as cdr.  */