]> git.vpit.fr Git - perl/modules/indirect.git/blobdiff - indirect.xs
Clarify what indirect_find() does by renaming some variables
[perl/modules/indirect.git] / indirect.xs
index f36c179e8537e024c75b414f90bb1e4d1eeefc2f..494093e97cee9a2b1597b4d5efa8a84eae5fc548 100644 (file)
@@ -479,33 +479,59 @@ STATIC void indirect_map_delete(pTHX_ const OP *o) {
 
 /* --- Check functions ----------------------------------------------------- */
 
-STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
-#define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
- STRLEN len;
- const char *p, *r = SvPV_const(sv, len);
+STATIC STRLEN indirect_nextline(const char *s, STRLEN len) {
+ STRLEN i;
+
+ for (i = 0; i < len; ++i) {
+  if (s[i] == '\n') {
+   ++i;
+   while (i < len && s[i] == '\r')
+    ++i;
+   break;
+  }
+ }
 
- if (len >= 1 && *r == '$') {
-  ++r;
-  --len;
-  s = strchr(s, '$');
-  if (!s)
+ return i;
+}
+
+STATIC int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
+#define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
+ STRLEN name_len, line_len;
+ const char *name, *p, *t, *u;
+
+ name = SvPV_const(name_sv, name_len);
+ if (name_len >= 1 && *name == '$') {
+  ++name;
+  --name_len;
+  line_bufptr = strchr(line_bufptr, '$');
+  if (!line_bufptr)
    return 0;
  }
 
- p = s;
+ p = line_bufptr;
  while (1) {
-  p = strstr(p, r);
+  p = strstr(p, name);
   if (!p)
    return 0;
-  if (!isALNUM(p[len]))
+  if (!isALNUM(p[name_len]))
    break;
-  /* p points to a word that has r as prefix, skip the rest of the word */
-  p += len + 1;
+  /* p points to a word that has name as prefix, skip the rest of the word */
+  p += name_len + 1;
   while (isALNUM(*p))
    ++p;
  }
 
- *pos = p - SvPVX_const(PL_linestr);
+ t = SvPV_const(PL_linestr, line_len);
+ u = t;
+ while (t <= p) {
+  STRLEN i = indirect_nextline(t, line_len);
+  if (i >= line_len)
+   break;
+  u         = t;
+  t        += i;
+  line_len -= i;
+ }
+ *name_pos = p - u;
 
  return 1;
 }
@@ -784,7 +810,8 @@ STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
   /* When positions are identical, the method and the object must have the
    * same name. But it also means that it is an indirect call, as "foo->foo"
    * results in different positions. */
-  if (moi->pos <= ooi->pos) {
+  if (   moi->line < ooi->line
+      || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
    SV *file;
    dSP;